python 3.x - scrapy save different yield dic in same json object -


the codes have on scrapy shown below:

def parse(self, response):     quote in response.css('div.search-item '):         f = quote.css('a.stack::attr(href)').extract_first()         f = response.urljoin(f)         # print(f)         yield {             'text': quote.css('span.tags::text').extract_first(),             'title': quote.css('h3 em::text').extract_first(),              }         yield response.follow(f, self.parse_program)  def parse_program(self, response):     def extract_with_css(query):         return response.css(query).extract_first().strip()      yield {          'name': extract_with_css('div.headline h1::text'),} 

the result turn out like:

 {'text':"sdada",'title':"12321q"}  {'name':"sdasdsa"} 

i want make like:

{'text':"sdada",'title':"12321q",'name':"sdasdsa"} 

what should do?

you need use request meta this, , yield item when have data

def parse(self, response):     quote in response.css('div.search-item '):         f = quote.css('a.stack::attr(href)').extract_first()         f = response.urljoin(f)         # print(f)         data = {             'text': quote.css('span.tags::text').extract_first(),             'title': quote.css('h3 em::text').extract_first(),              }         yield response.follow(f, self.parse_program, meta={"data": data})  def parse_program(self, response):     def extract_with_css(query):         return response.css(query).extract_first().strip()     data = response.meta["data"]     data['name'] = extract_with_css('div.headline h1::text')     yield data 

Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -