【发布时间】:2020-06-10 18:08:21
【问题描述】:
我将尝试抽象我的代码,因为它有点大。
所以这个函数是用来解析论坛帖子的
def parse_thread_next_pages(self,response):
print("----- Scraping new NEXT THREAD PAGE ------")
for r in replies_body:
#Here I'm doing some parsing and adding to 'myitem'
if absolute_next_page_url=="javascript:;":
#We reached the last page, myitem contains all the information I need, I want to break and return it
break
else:
#This yield is used to call this function recursively for each pages
yield scrapy.Request(absolute_next_page_url, callback=self.parse_thread_next_pages,meta={'myitem': myitem})
return myitem
问题是,当我做scrapy crawl spider -t json -o result.json 时,它是空的。但是,如果我注释掉 yield 行,它就可以工作。但显然我没有得到预期的结果,因为这个解析函数没有被递归调用。
为什么会发生这种情况?在递归调用此函数并到达最后一页后如何返回我的项目?
【问题讨论】:
标签: python python-3.x web-scraping scrapy web-crawler