mirror of https://github.com/scrapy/scrapy.git
Updated input/ouput processor example according to #560.
This commit is contained in:
parent
91387b6e55
commit
e441a01818
|
|
@ -182,21 +182,32 @@ output processors to use: in the :ref:`Item Field <topics-items-fields>`
|
|||
metadata. Here is an example::
|
||||
|
||||
import scrapy
|
||||
from scrapy.contrib.loader.processor import MapCompose, Join, TakeFirst
|
||||
from w3lib.html import remove_entities
|
||||
from myproject.utils import filter_prices
|
||||
from scrapy.contrib.loader.processor import Join, MapCompose, TakeFirst
|
||||
from w3lib.html import remove_tags
|
||||
|
||||
def filter_price(value):
|
||||
if value.isdigit():
|
||||
return value
|
||||
|
||||
class Product(scrapy.Item):
|
||||
name = scrapy.Field(
|
||||
input_processor=MapCompose(remove_entities),
|
||||
input_processor=MapCompose(remove_tags),
|
||||
output_processor=Join(),
|
||||
)
|
||||
price = scrapy.Field(
|
||||
default=0,
|
||||
input_processor=MapCompose(remove_entities, filter_prices),
|
||||
input_processor=MapCompose(remove_tags, filter_price),
|
||||
output_processor=TakeFirst(),
|
||||
)
|
||||
|
||||
::
|
||||
|
||||
>>> from scrapy.contrib.loader import ItemLoader
|
||||
>>> il = ItemLoader(item=Product())
|
||||
>>> il.add_value('name', [u'Welcome to my', u'<strong>website</strong>'])
|
||||
>>> il.add_value('price', [u'€', u'<span>1000</span>'])
|
||||
>>> il.load_item()
|
||||
{'name': u'Welcome to my website', 'price': u'1000'}
|
||||
|
||||
The precedence order, for both input and output processors, is as follows:
|
||||
|
||||
1. Item Loader field-specific attributes: ``field_in`` and ``field_out`` (most
|
||||
|
|
|
|||
Loading…
Reference in New Issue