Suppose I have a Bookitem, I need to add information to it in both the parse phase and detail phase
JavaScript
x
13
13
1
def parse(self, response)
2
data = json.loads(response)
3
for book in data['result']:
4
item = BookItem();
5
item['id'] = book['id']
6
url = book['url']
7
yield Request(url, callback=self.detail)
8
9
def detail(self,response):
10
hxs = HtmlXPathSelector(response)
11
item['price'] =
12
#I want to continue the same book item as from the for loop above
13
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.
Advertisement
Answer
There is an argument named meta
for Request:
JavaScript
1
2
1
yield Request(url, callback=self.detail, meta={'item': item})
2
then in function detail
, access it this way:
JavaScript
1
2
1
item = response.meta['item']
2
See more details here about jobs topic.