python - 有没有办法修复引用地址: none for a 301 error?

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

我对 scrapy 比较陌生,我想知道是否有办法将引用者传递给 response.follow() 命令。我正在尝试从网站上的大量内容中获取房地产土地价格,但我无法让 scrapy 跟踪分页链接。抓取工具在主页上运行良好,但该网站不允许它访问任何其他页面。

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

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

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

-谢谢

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 作为字符串。它不接受 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)

关于python - 有没有办法修复引用地址: none for a 301 error?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54153884/

相关文章:

python - numpy.array() 的速记符号

Python:BeautifulSoup从div部分提取所有span类

dom - Youtube 元数据和 opengraph 标签

python - 在 Selenium 中从父元素中提取特定的子元素

python - 如何在XPath中选择多个标签?

python - Scrapy 蜘蛛过早关闭

python - 在 Scrapy 中嵌套项目数据

python - python中的数组乘法

python - 在 Pandas 中过滤数据框时复制警告

python - celery worker 无法连接到 docker 实例上的 redis