gpt4 book ai didi

python - Scrapy - 无法在 XMLFeedSpider 中发出额外请求

转载 作者:太空宇宙 更新时间:2023-11-04 02:47:52 24 4
gpt4 key购买 nike

我有一个使用 XMLFeedSpider 的 scrapy 蜘蛛。除了在 parse_node() 中为每个节点返回的数据外,我还需要进行额外的请求以获取更多数据。唯一的问题是,如果我从 parse_node() 产生一个额外的请求,则什么都不会返回:

class MySpidersSpider(XMLFeedSpider):
name = "myspiders"
namespaces = [('g', 'http://base.google.com/ns/1.0')]
allowed_domains = {"www.myspiders.com"}
start_urls = [
"https://www.myspiders.com/productMap.xml"
]
iterator = 'iternodes'
itertag = 'item'

def parse_node(self, response, node):
if(self.settings['CLOSESPIDER_ITEMCOUNT'] and int(self.settings['CLOSESPIDER_ITEMCOUNT']) == self.item_count):
raise CloseSpider('CLOSESPIDER_ITEMCOUNT limit reached - ' + str(self.settings['CLOSESPIDER_ITEMCOUNT']))
else:
self.item_count += 1
id = node.xpath('id/text()').extract()
title = node.xpath('title/text()').extract()
link = node.xpath('link/text()').extract()
image_link = node.xpath('g:image_link/text()').extract()
gtin = node.xpath('g:gtin/text()').extract()
product_type = node.xpath('g:product_type/text()').extract()
price = node.xpath('g:price/text()').extract()
sale_price = node.xpath('g:sale_price/text()').extract()
availability = node.xpath('g:availability/text()').extract()

item = MySpidersItem()
item['id'] = id[0]
item['title'] = title[0]
item['link'] = link[0]
item['image_link'] = image_link[0]
item['gtin'] = gtin[0]
item['product_type'] = product_type[0]
item['price'] = price[0]
item['sale_price'] = '' if len(sale_price) == 0 else sale_price[0]
item['availability'] = availability[0]

yield Request(item['link'], callback=self.parse_details, meta={'item': item})

def parse_details(self, response):
item = response.meta['item']
item['price_per'] = 'test'
return item

如果我将 parse_node() 的最后一行更改为 return item 它工作正常(当然没有在项目中设置 price_per ) .

知道我做错了什么吗?

最佳答案

您是否尝试检查 item['link'] 的内容?如果它是一个相对链接(例如:/products?id=5),该 URL 将不会返回任何内容并且请求将失败。您需要确保它是一个可解析的链接(例如:https://www.myspiders.com/products?id=5)。

关于python - Scrapy - 无法在 XMLFeedSpider 中发出额外请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44635930/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com