【问题标题】:Is there a way to fix the referrer: none for a 301 error?有没有办法修复推荐人:301 错误没有?
【发布时间】:2019-01-11 20:52:16
【问题描述】:

我对scrapy 比较陌生,我想知道是否有办法将引荐来源网址传递给response.follow() 命令。我正试图从满是网站的手中刮取房地产土地价格,但我很难按照分页链接进行抓取。刮板在主页上工作正常,但该网站不允许它访问任何其他页面。

我尝试在 scrapy shell 中使用 fetch 命令直接打开第二页,但没有成功。我使用视图打开页面检查元素,发现以下错误:

“CORS 策略已阻止从源 'null' 访问位于 'https://someaddress.com 的 XMLHttpRequest:请求的资源上不存在 'Access-Control-Allow-origin' 标头。”

任何建议或资源将不胜感激。

-谢谢

import scrapy


class cwSpider(scrapy.Spider):
    name = 'cushman2'
    custom_settings = {
        'DUPEFILTER_DEBUG': 'True',
    }
    start_urls = ['https://cwstevenson.ca/properties/advance-search-properties/']
    def parse(self, response):
        # follow links to author pages
        for href in response.css('.wpl_prp_bot a::attr(href)'):
            yield response.follow(href, self.parse_property)

        # follow pagination links
        for href in response.css('li.next a::attr(href)'):
            yield response.follow(href, self.parse)

    def parse_property(self, response):
        response.request.headers.get('Referrer', None)
        def extract_with_css(query):
            return response.css(query).extract()

        yield {
            'address' : extract_with_css('h1.title_text::text'),
            'Prop_Type': extract_with_css('.ldetailscont2 p.ldetailsinfo::text')[0],
            'Land Area': extract_with_css('.ldetailscont2 p.ldetailsinfo::text')[1],
            'Price': extract_with_css('.ldetailscont2 p.ldetailsinfo::text')[2],
            'Listing_Type': extract_with_css('.ldetailscont2 p.ldetailsinfo::text')[3],
            'Area_Avail': extract_with_css('.ldetailscont2 p.ldetailsinfo::text')[4],
            'Prop_Taxes': extract_with_css('.ldetailscont2 p.ldetailsinfo::text')[5],
        }

【问题讨论】:

    标签: python web-scraping scrapy http-status-code-301 referrer


    【解决方案1】:

    您需要将链接提取为字符串,否则它会返回selectors 的列表。
    response.follow 需要url 作为字符串。它不接受 selector 对象列表。
    因为response.follow 没有收到有效的参数 - 它不执行下一个请求

    def parse(self, response):
        # follow links to author pages
        for href in response.css('.wpl_prp_bot a::attr(href)').extract():   #
            yield response.follow(href, self.parse_property)
    
        # follow pagination links
        for href in response.css('li.next a::attr(href)').extract():   #
            yield response.follow(href, self.parse)
    

    【讨论】:

    • 这并没有解决问题。经过大量的谷歌搜索和浏览网站的标题后,我找到了解决这个特定问题的方法。请看我的回答,让我知道你的想法。
    【解决方案2】:

    使用 .follow 行中的 header 参数,您可以将任何相关信息传递给 follow 命令以满足站点标题。在这种情况下,它是推荐人。要了解站点标题,您可以在 chrome 中打开您的开发人员选项卡,转到网络选项卡,然后转到 XHR 选项卡(确保在加载站点时打开它,如果不只是刷新页面一次它打开)单击标题选项卡并向下滚动,直到您在标题部分看到引用者。您可以使用它来填充您的爬虫中的标题。我相信有一些选项可以通过你的中间件来做到这一点,但似乎没有一个选项可以满足这个问题的要求。(即:same_origin、origin...ect)

    def parse(self, response):
            # follow links to author pages
            for href in response.css('.wpl_prp_bot a::attr(href)').extract():
                yield response.follow(href, self.parse_property, headers = {'User-Agent': 'Chrome/71.0.3578.98', "Referer": href})
    
            # follow pagination links
            for href in response.css('li.next a::attr(href)').extract():
                yield response.follow(href, self.parse,headers = {'User-Agent': 'Chrome/71.0.3578.98', "Referer": href})
    

    【讨论】:

      猜你喜欢
      • 2012-07-11
      • 1970-01-01
      • 1970-01-01
      • 2022-08-04
      • 2019-10-10
      • 1970-01-01
      • 1970-01-01
      • 2011-07-31
      • 1970-01-01
      相关资源
      最近更新 更多