Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.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_Scrapy - Fatal编程技术网

使用Python递归地抓取页面(scrapy)

使用Python递归地抓取页面(scrapy),python,python-3.x,scrapy,Python,Python 3.x,Scrapy,我试图制作一个程序,在进入下一页时检索项目的标题和价格 现在,第一页的所有信息(标题、价格)都已提取,但程序不会转到下一页 网址: 在这种情况下,您应该添加执行日志,这将有助于确定您的问题 不过,我可以看到一些问题: next_page_url = response.xpath("//li[@class='page-item']//a/@href") if next_page_url: absolute_nextpage

我试图制作一个程序,在进入下一页时检索项目的标题和价格

现在,第一页的所有信息(标题、价格)都已提取,但程序不会转到下一页

网址:


在这种情况下,您应该添加执行日志,这将有助于确定您的问题

不过,我可以看到一些问题:

        next_page_url = response.xpath("//li[@class='page-item']//a/@href")
        if next_page_url:
            absolute_nextpage_url = response.urljoin(next_page_url)
变量
next\u page\u url
包含选择器,而不是字符串。您需要使用
.get()
方法提取带有相对url的字符串

在此之后,我执行了它返回的代码:

2020-09-04 15:19:34 [scrapy.spidermiddlewares.offsite] DEBUG: Filtered offsite request to 'scrapingclub.com': <GET https://scrapingclub.com/exercise/list_basic/?page=2>
  • 如果使用
    get()
  • 如果您使用
    getall()
    它将返回一个列表,您需要对该列表进行迭代以生成所有可能的请求,但这是一个递归函数,因此您最终会在每个递归步骤中执行该操作
最好的选择是选择“下一步”按钮而不是页码:

next_page_url = response.xpath('//li[@class="page-item"]/a[contains(text(), "Next")]/@href').get()

谢谢你的回答!
        next_page_url = response.xpath("//li[@class='page-item']//a/@href").get() # I added the .get()
        if next_page_url:
            absolute_nextpage_url = response.urljoin(next_page_url)
            yield scrapy.Request(absolute_nextpage_url) 
next_page_url = response.xpath('//li[@class="page-item"]/a[contains(text(), "Next")]/@href').get()