python - How to return data in separated function form the scrapy parse function? -


i have parse function using scrapy data website, function pass data function withen function, can not return output!, tried print worked need use return data in json file. how make printed func return items?

def parse(self, response):      all_tr= list()      tr in response.xpath('//tr').extract():            all_tr.append(tr)      tr_data = list()      city_tr = list()      tr in all_tr:            if re.findall(r'class="city".+name.+?<', tr):              city_tr.append(tr)            else:              pass      c= 0      const =1      while const ==1:           try:             start=city_tr[c]             end= city_tr[c+1]             indexstart=all_tr.index(start)             indexend=all_tr.index(end)             tr_data.append(all_tr[indexstart:indexend])             c=c+1           except indexerror:             const=2             tr_data.append(all_tr[all_tr.index(start):])      tr in tr_data:           func_2(tr)  

i have operations in func_2, passing fnction "prin func"

def print_func(city,days, tr):     if len(days)==0:         item=propertiesitem()         item['name']= ""         item['city']= city         item['state']= "ca"         return item 

print_func not return anything, please tell why?

scrapy checks return value of parse function. if call function inside , discard return value don't return parse function such

so code should change

for tr in tr_data:     func_2(tr)  

to

for tr in tr_data:     yield func_2(tr)  

this make sure whatever item func_2 returns passed scrapy framework. in func_2 function calling print_func, need capture return value , pass calling function

def func_2(....):     ....     item = print_func(...)     ...     return item 

you need value in parse function , yield there. returning function doesn't work.


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -