- 如何使用蜘蛛网中的物品?
好吧,项目的主要目的是存储你爬网的数据。
scrapy.Items基本上是字典。要声明你的物品,你将必须创建一个类并添加一个类
scrapy.Field:
import scrapyclass Product(scrapy.Item): url = scrapy.Field() title = scrapy.Field()
现在,你可以通过导入产品在蜘蛛中使用它。
有关高级信息,我让你在此处检查文档
- 如何将项目发送到管道?
首先,你需要告诉spider使用
custom pipeline。
在settings.py文件中:
ITEM_PIPELINES = { 'myproject.pipelines.CustomPipeline': 300,}你现在可以编写管道并处理你的项目。
在pipeline.py文件中:
from scrapy.exceptions import DropItemclass CustomPipeline(object): def __init__(self): # Create your database connection def process_item(self, item, spider): # Here you can index your item return item
最后,在你的Spider中,你需要在
yield填充物品后对其进行操作。
spider.py示例:
import scrapyfrom myspider.items import Productclass MySpider(scrapy.Spider): name = "test" start_urls = [ 'http://www.exemple.com', ]def parse(self, response): doc = Product() doc['url'] = response.url doc['title'] = response.xpath('//div/p/text()') yield doc # Will go to your pipeline


