Scrapy: Passing item between methods

前端 未结 2 516
一个人的身影
一个人的身影 2020-12-24 14:46

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)         


        
相关标签:
2条回答
  • 2020-12-24 15:06

    You can define variable in init method:

    class MySpider(BaseSpider):
        ...
    
        def __init__(self):
            self.item = None
    
        def parse(self, response)
            data = json.loads(response)
            for book in data['result']:
                self.item = BookItem();
                self.item['id'] = book['id']
                url = book['url']
                yield Request(url, callback=self.detail)
    
        def detail(self, response):        
            hxs = HtmlXPathSelector(response)
            self.item['price'] = ....
    
    0 讨论(0)
  • 2020-12-24 15:09

    There is an argument named meta for Request:

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

    then in function detail, access it this way:

    item = response.meta['item']
    

    See more details here about jobs topic.

    0 讨论(0)
提交回复
热议问题