簡體   English   中英

Python Scrapy - scrapy.請求不工作

[英]Python Scrapy - scrapy.Request not working

我的抓取器抓取了 0 個頁面,我認為問題出在 parse 方法的最后一行代碼中:

def parse(self, response):
        all_companies = response.xpath('//header[@class = "card-header"]')

        for company in all_companies:
            company_url = company.xpath('./a[@class = "card-header-scorecard"]/@href').extract_first()
            yield scrapy.Request(url=company_url, callback = self.parse_company)

我用碎片 shell 測試了 company_url 的檢索,它們都正確返回。 抓取器訪問每個 url 並使用 parse_company 方法抓取項目。

在使用 yield 之前,我使用的是 Rule 功能,它與 parse_company 一起工作得很好,所以我知道這個方法有效,但是我不得不改變我的方法。

rules = (
    Rule(LinkExtractor(restrict_css=".card-header > a"), callback="parse_company")
)

您正在使用CrawlSpider ,在 scrapy 的最新版本中,CrawlSpider 的默認回調是_parse而不是parse 如果你想覆蓋默認回調然后使用_parse或者你可以使用scrapy.Spider而不是scrapy.CrawlSpider

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM