本文介绍了Scrapy:在方法之间传递项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个Bookitem,我需要在解析阶段和细节阶段给它添加信息

Suppose I have a Bookitem, I need to add information to it in both the parse phase and detail phase

def parse(self, response)
    data = json.loads(response)
    for book in data['result']:
        item = BookItem();
        item['id'] = book['id']
        url = book['url']
        yield Request(url, callback=self.detail)

def detail(self,response):
    hxs = HtmlXPathSelector(response)
    item['price'] = ......
#I want to continue the same book item as from the for loop above

按原样使用代码会导致详细信息阶段出现未定义的项目.如何将项目传递给详细信息?detail(self,response,item) 似乎不起作用.

Using the code as is would led to undefined item in the detail phase. How can I pass the item to the detail? detail(self,response,item) doesn't seem to work.

推荐答案

Request 有一个名为 meta 的参数:

There is an argument named meta for Request:

yield Request(url, callback=self.detail, meta={'item': item})

然后在函数detail中,这样访问:

then in function detail, access it this way:

item = response.meta['item']

此处查看有关职位主题的更多详细信息.

See more details here about jobs topic.

这篇关于Scrapy:在方法之间传递项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 11:45