好吧,items 的主要目的是存储你爬取的数据。scrapy.Items
基本上都是字典。要声明您的项目,您必须创建一个类并添加scrapy.Field
in it:
import scrapy
class Product(scrapy.Item):
url = scrapy.Field()
title = scrapy.Field()
您现在可以通过导入您的产品在蜘蛛中使用它。
有关高级信息,我让您查看文档here https://doc.scrapy.org/en/latest/topics/items.html?#module-scrapy.item
首先,你需要告诉你的蜘蛛使用你的custom pipeline
.
In the 设置.py file:
ITEM_PIPELINES = {
'myproject.pipelines.CustomPipeline': 300,
}
您现在可以编写管道并使用您的项目。
In the 管道.py file:
from scrapy.exceptions import DropItem
class 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
您的物品一旦装满。
蜘蛛.py例子:
import scrapy
from myspider.items import Product
class 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
希望这有帮助,这是文档管道: 项目管道 https://doc.scrapy.org/en/latest/topics/item-pipeline.html