抓取单个蜘蛛将多个项目类传递给管道 [英] scrapy single spider to pass multiple item classes to pipeline
本文介绍了抓取单个蜘蛛将多个项目类传递给管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是scrapy的新手.在 items.py 中,我声明了 2 个名为 ItemClass1 和 ItemClass2 的 ItemClass.蜘蛛方法 parseUrl 获取 html 并抓取数据并放入相应项目类的列表中.
I am new to scrapy. In items.py, I declare 2 ItemClass called ItemClass1 and ItemClass2. A spider method parseUrl get the html and scrape data and put into lists for respective Item Classes.
e.g:
C1Items = []
C1Item = ItemClass1()
#scrape data
C1Items.append(C1Item)
...
C2Items = []
C2Item = ItemClass2()
#scrape data
C2Items.append(C2Item)
...
最后:C1Items 和 C2Items 包含所需的数据.
finally: C1Items and C2Items contain required data.
return C1Items #will pass ItemClass1 data to pipeline
return C2Items #will pass ItemClass2 data to pipeline
您能否告知将 C1Items、C2Items 传递给管道的最佳方法是什么.
Could you please advise what is the best way to pass both C1Items, C2Items to pipeline.
推荐答案
要么将不同类的所有项目组合成一个列表并返回该列表,要么使用 yield
语句:
Either combine all the items of different classes into one list and return that list, or use yield
statement:
C1Item = ItemClass1()
#scrape data
yield C1Item
...
C2Item = ItemClass2()
#scrape data
yield C2Item
这篇关于抓取单个蜘蛛将多个项目类传递给管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文