2009-01-03 09:14:52 +00:00
|
|
|
.. _topics-item-pipeline:
|
|
|
|
|
2009-01-03 03:05:19 +00:00
|
|
|
=============
|
|
|
|
Item Pipeline
|
|
|
|
=============
|
|
|
|
|
2010-08-21 01:26:35 -03:00
|
|
|
After an item has been scraped by a spider, it is sent to the Item Pipeline
|
2009-01-03 03:05:19 +00:00
|
|
|
which process it through several components that are executed sequentially.
|
|
|
|
|
2010-10-10 20:31:05 -02:00
|
|
|
Each item pipeline component (sometimes referred as just "Item Pipeline") is a
|
|
|
|
Python class that implements a simple method. They receive an Item and perform
|
|
|
|
an action over it, also deciding if the Item should continue through the
|
|
|
|
pipeline or be dropped and no longer processed.
|
2009-01-03 03:05:19 +00:00
|
|
|
|
2010-10-10 20:31:05 -02:00
|
|
|
Typical use for item pipelines are:
|
|
|
|
|
|
|
|
* cleansing HTML data
|
|
|
|
* validating scraped data (checking that the items contain certain fields)
|
|
|
|
* checking for duplicates (and dropping them)
|
|
|
|
* storing the scraped item in a database
|
2009-01-03 03:05:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
Writing your own item pipeline
|
|
|
|
==============================
|
|
|
|
|
|
|
|
Writing your own item pipeline is easy. Each item pipeline component is a
|
2010-08-12 10:48:37 -03:00
|
|
|
single Python class that must implement the following method:
|
2009-01-03 03:05:19 +00:00
|
|
|
|
2010-08-12 10:48:37 -03:00
|
|
|
.. method:: process_item(item, spider)
|
2009-01-03 03:05:19 +00:00
|
|
|
|
2010-08-12 10:48:37 -03:00
|
|
|
This method is called for every item pipeline component and must either return
|
|
|
|
a :class:`~scrapy.item.Item` (or any descendant class) object or raise a
|
|
|
|
:exc:`~scrapy.exceptions.DropItem` exception. Dropped items are no longer
|
|
|
|
processed by further pipeline components.
|
2009-01-07 03:59:39 +00:00
|
|
|
|
2009-11-30 11:04:15 -02:00
|
|
|
:param item: the item scraped
|
|
|
|
:type item: :class:`~scrapy.item.Item` object
|
2009-01-03 03:05:19 +00:00
|
|
|
|
2010-08-12 10:48:37 -03:00
|
|
|
:param spider: the spider which scraped the item
|
2013-12-28 00:47:32 +06:00
|
|
|
:type spider: :class:`~scrapy.spider.Spider` object
|
2010-08-12 10:48:37 -03:00
|
|
|
|
|
|
|
Additionally, they may also implement the following methods:
|
|
|
|
|
|
|
|
.. method:: open_spider(spider)
|
|
|
|
|
|
|
|
This method is called when the spider is opened.
|
|
|
|
|
|
|
|
:param spider: the spider which was opened
|
2013-12-28 00:47:32 +06:00
|
|
|
:type spider: :class:`~scrapy.spider.Spider` object
|
2010-08-12 10:48:37 -03:00
|
|
|
|
|
|
|
.. method:: close_spider(spider)
|
|
|
|
|
|
|
|
This method is called when the spider is closed.
|
|
|
|
|
|
|
|
:param spider: the spider which was closed
|
2013-12-28 00:47:32 +06:00
|
|
|
:type spider: :class:`~scrapy.spider.Spider` object
|
2009-01-03 03:05:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
Item pipeline example
|
|
|
|
=====================
|
|
|
|
|
2010-10-10 20:31:05 -02:00
|
|
|
Price validation and dropping items with no prices
|
|
|
|
--------------------------------------------------
|
|
|
|
|
2013-01-22 14:52:18 -08:00
|
|
|
Let's take a look at the following hypothetical pipeline that adjusts the ``price``
|
2009-01-03 03:05:19 +00:00
|
|
|
attribute for those items that do not include VAT (``price_excludes_vat``
|
|
|
|
attribute), and drops those items which don't contain a price::
|
|
|
|
|
2010-08-10 17:36:48 -03:00
|
|
|
from scrapy.exceptions import DropItem
|
2009-01-03 03:05:19 +00:00
|
|
|
|
|
|
|
class PricePipeline(object):
|
|
|
|
|
|
|
|
vat_factor = 1.15
|
|
|
|
|
2010-08-12 10:48:37 -03:00
|
|
|
def process_item(self, item, spider):
|
2009-08-19 21:39:58 -03:00
|
|
|
if item['price']:
|
|
|
|
if item['price_excludes_vat']:
|
|
|
|
item['price'] = item['price'] * self.vat_factor
|
2009-01-03 03:05:19 +00:00
|
|
|
return item
|
|
|
|
else:
|
|
|
|
raise DropItem("Missing price in %s" % item)
|
|
|
|
|
2009-01-29 19:18:03 +00:00
|
|
|
|
2010-10-10 20:31:05 -02:00
|
|
|
Write items to a JSON file
|
|
|
|
--------------------------
|
|
|
|
|
|
|
|
The following pipeline stores all scraped items (from all spiders) into a a
|
|
|
|
single ``items.jl`` file, containing one item per line serialized in JSON
|
|
|
|
format::
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
|
|
|
class JsonWriterPipeline(object):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.file = open('items.jl', 'wb')
|
|
|
|
|
|
|
|
def process_item(self, item, spider):
|
|
|
|
line = json.dumps(dict(item)) + "\n"
|
|
|
|
self.file.write(line)
|
|
|
|
return item
|
|
|
|
|
|
|
|
.. note:: The purpose of JsonWriterPipeline is just to introduce how to write
|
|
|
|
item pipelines. If you really want to store all scraped items into a JSON
|
|
|
|
file you should use the :ref:`Feed exports <topics-feed-exports>`.
|
|
|
|
|
2012-08-28 18:31:03 -03:00
|
|
|
Duplicates filter
|
|
|
|
-----------------
|
2009-01-29 19:18:03 +00:00
|
|
|
|
2012-08-28 18:31:03 -03:00
|
|
|
A filter that looks for duplicate items, and drops those items that were
|
|
|
|
already processed. Let say that our items have an unique id, but our spider
|
|
|
|
returns multiples items with the same id::
|
2009-01-29 19:18:03 +00:00
|
|
|
|
|
|
|
|
2010-08-10 17:36:48 -03:00
|
|
|
from scrapy.exceptions import DropItem
|
2009-01-29 19:18:03 +00:00
|
|
|
|
|
|
|
class DuplicatesPipeline(object):
|
|
|
|
|
2012-08-28 18:31:03 -03:00
|
|
|
def __init__(self):
|
|
|
|
self.ids_seen = set()
|
2009-01-29 19:18:03 +00:00
|
|
|
|
2010-08-12 10:48:37 -03:00
|
|
|
def process_item(self, item, spider):
|
2012-08-28 18:31:03 -03:00
|
|
|
if item['id'] in self.ids_seen:
|
2009-01-29 19:18:03 +00:00
|
|
|
raise DropItem("Duplicate item found: %s" % item)
|
|
|
|
else:
|
2012-08-28 18:31:03 -03:00
|
|
|
self.ids_seen.add(item['id'])
|
2009-01-29 19:18:03 +00:00
|
|
|
return item
|
2012-08-28 18:31:03 -03:00
|
|
|
|
|
|
|
|
|
|
|
Activating an Item Pipeline component
|
|
|
|
=====================================
|
|
|
|
|
|
|
|
To activate an Item Pipeline component you must add its class to the
|
2013-09-23 16:41:58 -03:00
|
|
|
:setting:`ITEM_PIPELINES` setting, like in the following example::
|
2012-08-28 18:31:03 -03:00
|
|
|
|
2013-09-23 16:41:58 -03:00
|
|
|
ITEM_PIPELINES = {
|
|
|
|
'myproject.pipeline.PricePipeline': 300,
|
|
|
|
'myproject.pipeline.JsonWriterPipeline': 800,
|
|
|
|
}
|
2013-11-19 16:12:54 -06:00
|
|
|
|
|
|
|
The integer values you assign to classes in this setting determine the
|
2013-11-19 17:51:50 -06:00
|
|
|
order they run in- items go through pipelines from order number low to
|
|
|
|
high. It's customary to define these numbers in the 0-1000 range.
|
2013-11-19 16:12:54 -06:00
|
|
|
|