Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.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 有没有办法修复推荐人:301错误无?_Python_Web Scraping_Scrapy_Http Status Code 301_Referrer - Fatal编程技术网

Python 有没有办法修复推荐人:301错误无?

Python 有没有办法修复推荐人:301错误无?,python,web-scraping,scrapy,http-status-code-301,referrer,Python,Web Scraping,Scrapy,Http Status Code 301,Referrer,我对scrapy比较陌生,我想知道是否有办法将推荐人传递给response.follow()命令。我正试图从一手的网站上刮取房地产地价,但我很难按照分页链接来刮。刮板可以在主页上正常工作,但网站不允许它访问任何其他页面 我试图在scrapy shell中使用fetch命令直接打开第二页,但没有成功。我使用视图打开页面检查元素,发现以下错误: “CORS策略已阻止在'from origin'null'访问XMLHttpRequest:请求的资源上不存在'Access Control Allow o

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

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

“CORS策略已阻止在'from origin'null'访问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],
        }

您需要将链接提取为字符串,否则它将返回
选择器的列表。
response.follow
需要
url
作为字符串。它不接受
选择器
对象列表。
as
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)

使用.follow行中的header参数,您可以将任何相关信息传递给follow命令以满足站点标题。在这种情况下,是推荐人。要了解站点标题,您可以在chrome中打开“开发者”选项卡,进入“网络”选项卡,然后进入“XHR”选项卡(确保在加载站点时此选项卡处于打开状态,如果不只是在打开页面后刷新页面),单击“标题”选项卡并向下滚动,直到在标题部分下看到referrer。您可以使用它来填充sprider中的标题。我相信可以通过中间件实现这一点,但是没有一个选项能够满足这个问题的要求


这似乎无法解决问题。在大量的谷歌搜索和浏览网站的标题后,我找到了这个特殊问题的解决方案。请看我的答案,让我知道你的想法。
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})