Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何用Scrapy爬行所有页面_Python_Python 3.x_Web Scraping_Scrapy - Fatal编程技术网

Python 如何用Scrapy爬行所有页面

Python 如何用Scrapy爬行所有页面,python,python-3.x,web-scraping,scrapy,Python,Python 3.x,Web Scraping,Scrapy,我正试图删除所有与本网站形成相关的链接: 因此,首先,我点击了这个脚本,只是为了测试一下,看看我是否可以在第一页中删除链接: import scrapy class LinkSpider(scrapy.Spider): name = "link" #allow_domains = ['https://www.formatic-centre.fr/'] start_urls = ['https://www.formatic-centre.fr/for

我正试图删除所有与本网站形成相关的链接:

因此,首先,我点击了这个脚本,只是为了测试一下,看看我是否可以在第一页中删除链接:

import scrapy


class LinkSpider(scrapy.Spider):
    name = "link"
    #allow_domains = ['https://www.formatic-centre.fr/']
    start_urls = ['https://www.formatic-centre.fr/formation/']

    #rules = (Rule(LinkExtractor(allow=r'formation'), callback="parse", follow= True),)

    def parse(self, response):
        card = response.xpath('//a[@class="title"]')
        for a in card:
            yield {'links': a.xpath('@href').get()}
成功了,我得到了这个:

[
{"links": "https://www.formatic-centre.fr/formation/les-regles-juridiques-du-teletravail/"},
{"links": "https://www.formatic-centre.fr/formation/mieux-gerer-son-stress-en-periode-du-covid-19/"},
{"links": "https://www.formatic-centre.fr/formation/dynamiser-vos-equipes-special-post-confinement/"},
{"links": "https://www.formatic-centre.fr/formation/conduire-ses-entretiens-specifique-post-confinement/"},
{"links": "https://www.formatic-centre.fr/formation/cours-excel/"},
{"links": "https://www.formatic-centre.fr/formation/autocad-3d-2/"},
{"links": "https://www.formatic-centre.fr/formation/concevoir-et-developper-une-strategie-marketing/"},
{"links": "https://www.formatic-centre.fr/formation/preparer-soutenance/"},
{"links": "https://www.formatic-centre.fr/formation/mettre-en-place-une-campagne-adwords/"},
{"links": "https://www.formatic-centre.fr/formation/utiliser-google-analytics/"}
]
但是当我想抓取所有的页面时,东西会变脏。。我迷路了,我的脚本不再工作了,我想我的循环不太正确,因为我有doublon等等

这是我最后的剧本:

import scrapy
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
from lxml import html

class LinkSpider(scrapy.Spider):
    name = "link"
    #allow_domains = ['https://www.formatic-centre.fr/']
    start_urls = ['https://www.formatic-centre.fr/formation/']

    rules = (Rule(LinkExtractor(allow=r'formation'), callback="parse", follow= True),)

    def parse(self, response):
        card = response.xpath('//a[@class="title"]')
        for a in card:
            yield {'links': a.xpath('@href').get()}



        next_page = response.xpath('.//a[@class="pagination__next btn-squae"]/@href').extract_first()
        if next_page:
            yield scrapy.Request(
                response.urljoin(next_page),
                callback=self.parse
            )

  
也许这是我的路?我检查并再次检查“下一步按钮”将把好的href放在哪里,我将:
//a[@class=“pagination\uu next btn squae”]/@href

但奇怪的是,html源代码中的链接没有链接到第二页,所以我很困惑

此处->

有什么想法吗


编辑:显然我需要一些
FormRequest
,我需要使用这种代码吗

接下来的页面将使用AJAX动态加载。您必须使用scrapy的
FormRequest
模拟这些请求

我建议你读一读

总之,您必须使用浏览器的开发人员工具来观察请求是如何(以及在何处)发出的,复制表单(有时还需要标题)并生成
FormRequest

编辑:
  • 打开页面
  • 打开浏览器开发工具(或检查某些元素)
  • 单击网络选项卡。按“XHR”过滤
  • 然后单击页面上的“下一步”按钮
  • 您应该会看到一个新的请求,如图所示:
选择请求。边数据是此请求的详细信息

矩形显示浏览器发出的请求的目的地。 下方有一个字段“Response Headers”(可见),在该字段下(您在图像中看不到),将有一个请求头,这些是您的浏览器用作请求头的参数。 有一个名为“Request”的选项卡,在那里您可以找到浏览器用于POST请求的formdata

您必须使用FormRequest并模拟浏览器发出的请求。从复制表单中的参数开始,如果不起作用,请在请求中包含标题

顺便说一下,这是Firefox。对于我提到的选项,其他浏览器可能有不同的位置。


这里的想法是Scrapy应该模拟浏览器的行为。大多数情况下,这是一个无痛的过程(一旦你明白你在做什么),但有时会非常痛苦,需要很长的时间,所以请放松。

好的,我会检查一下。我迷路了,我不知道是在哪里提出的请求,我发现了一些东西,但我甚至不确定那是什么。@LaurieFalcon我编辑了我的答案,请看一看。谢谢你详细的回答,但我不明白为什么我要用formdata,它只是参数。也许我没在正确的位置。当你点击下一步按钮时,它会向服务器发出POST请求。POST请求在其正文中包含一条由表单生成的消息。您看到的这些字段是在请求中发送的消息,因此您必须在Scrapy请求中包含这些数据。为此,您需要使用
FormRequest
,这是一种Scrapy方法。你似乎在这里遗漏了一些关键概念,因此你必须花一些时间阅读我发布的链接并搜索你不了解的内容。你想收集所有内部链接还是特定的?特定的。不是所有的链接,链接指的是谁的形成。就像上面的例子一样。(第一幅图)什么?每个页面的链接?不,不,每个表单的链接像这样:所有页面的所有链接