【问题标题】:Scrapy - Filtering offsite request but in allowed domains?Scrapy - 过滤异地请求但在允许的域中?
【发布时间】:2021-11-17 11:58:15
【问题描述】:

我有以下代码,想一步一步进入网站的下一页:

import scrapy

class ZoosSpider(scrapy.Spider):
    name = 'zoos'
    allowed_domains = ['https://www.tripadvisor.co.uk']
    start_urls = ['https://www.tripadvisor.co.uk/Attractions-g186216-Activities-c48-a_allAttractions.true-United_Kingdom.html']

    def parse(self, response):
        tmpSEC = response.xpath("//section[@data-automation='AppPresentation_SingleFlexCardSection']")
        for elem in tmpSEC:
          yield {
            "link": response.urljoin(elem.xpath(".//a/@href").get())
          }

        nextPage = response.xpath("//a[@aria-label='Next page']/@href").get()
        if nextPage != None:
          nextPage = response.urljoin(nextPage)
          yield scrapy.Request(nextPage, callback=self.parse)     

但是当我运行这段代码时,只有第一页被抓取,我得到这个错误消息:

2021-11-17 12:52:20 [scrapy.spidermiddlewares.offsite] DEBUG: Filtered offsite request to 'www.tripadvisor.co.uk': <GET https://www.tripadvisor.co.uk/ClientLink?value=NVB5Xy9BdHRyYWN0aW9ucy1nMTg2MjE2LUFjdGl2aXRpZXMtYzQ4LWFfYWxsQXR0cmFjdGlvbnMudHJ1ZS1vYTMwLVVuaXRlZF9LaW5nZG9tLmh0bWxfQ3Yx>

只有当我删除这一行时,我才会得到所有结果

allowed_domains = ['https://www.tripadvisor.co.uk']

为什么 - 指向以下站点的链接具有允许的域?

【问题讨论】:

  • allowed_domains 只接受域,不接受 URL。使用“www.tripadvisor.co.uk”

标签: python web-scraping scrapy


【解决方案1】:

默认蜘蛛 allowed_domains 不是强制性的。为了最大限度地减少错误,最好将其排除在外。 还有一点就是你可以删除allowed_domains或者你必须排除 https:// 表示您可以将 www.tripadvisor.co.uk 包含为 allowed_domains 根据scrapy doc。这就是为什么你会收到这个https:// 的错误 部分。

正确方法如下:

allowed_domains = ['www.tripadvisor.co.uk']

【讨论】:

    猜你喜欢
    • 2023-01-15
    • 2014-07-24
    • 2011-01-15
    • 2013-03-24
    • 1970-01-01
    • 2020-07-22
    • 2012-11-03
    • 1970-01-01
    • 2017-01-04
    相关资源
    最近更新 更多