我用 Scrapy 抓取了一个网站,它在没有分页的情况下运行良好。当我使用分页时,我有一个 KeyError。
抓取代码:
class AnnonceSpider(scrapy.Spider):
name = 'rzc_results'
def __init__(self, *args, **kwargs):
data_file = pkgutil.get_data(
"rzc_spider", "json/input/complete_rzc_scrape_rectif.json")
self.data = json.loads(data_file)
def start_requests(self):
for item in self.data:
request = scrapy.Request(item['rzc_url'], callback=self.parse)
request.meta['item'] = item
yield request
def parse(self, response):
item = response.meta['item']
item['results'] = []
for caritem in response.css("div.adCardOuter_d2sn17 > div[itemprop='item']"):
data = AnnonceItem()
#model
data["model"] = []
data["model"] = caritem.css("h2.title_16j3u81 > div::text").extract_first()
if len(data["model"]) == 0:
data["model"] = None
item['results'].append(data)
yield item
next_page = response.css(
'a.link_huvdae-o_O-linkPrevNext_1v3fox8::attr(href)').extract_first()
if next_page is not None:
url_pagination = 'https://www.url.com' + next_page
yield scrapy.Request(url=url_pagination, callback=self.parse)
错误信息:
File "/home/bolgi/anaconda3/envs/spiders/rzc_spider/rzc_spider/spiders/rzc_item.py", line 26, in parse
item = response.meta['item']
KeyError: 'item'
有什么问题,我该如何解决这个问题?
翻翻过去那场雪
白猪掌柜的
相关分类