Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

added intro/tutorial for new Item and ItemAdaptor

Ismael Carnalesff950e7aa9e6
diff --git a/scrapy/trunk/docs/proposed/index.rst b/scrapy/trunk/docs/proposed/index.rst
index 0fc8dfa1d..7989dedd7 100644
--- a/scrapy/trunk/docs/proposed/index.rst
+++ b/scrapy/trunk/docs/proposed/index.rst
@@ -11,7 +11,7 @@ Proposed documentation
.. toctree::
:maxdepth: 1
- items
+ newitem
spiders
.. toctree::
diff --git a/scrapy/trunk/docs/proposed/items.rst b/scrapy/trunk/docs/proposed/items.rst
deleted file mode 100644
index ec341f834..000000000
--- a/scrapy/trunk/docs/proposed/items.rst
+++ /dev/null
@@ -1,42 +0,0 @@
-.. _items:
-
-=====
-Items
-=====
-
-In Scrapy, Items are the placeholder to use for the scraped data. They are
-represented by a :class:`~scrapy.item.ScrapedItem` object, or any subclass
-instance, and store the information in instance attributes.
-
-.. module:: scrapy.item
-
-ScrapedItem
------------
-
-.. autoclass:: ScrapedItem(object)
- :members:
-
- .. automethod:: __init__
-
- :param data: A dictionary containing attributes and values to be set
- after instancing the item.
-
-Examples
---------
-
-Creating an item and setting some attributes::
-
- >>> from scrapy.item import ScrapedItem
- >>> item = ScrapedItem()
- >>> item.name = 'John'
- >>> item.last_name = 'Smith'
- >>> item.age = 23
- >>> item
- ScrapedItem({'age': 23, 'last_name': 'Smith', 'name': 'John'})
-
-Creating an item and setting its attributes inline::
-
- >>> person = ScrapedItem({'name': 'John', 'age': 23, 'last_name': 'Smith'})
- >>> person
- ScrapedItem({'age': 23, 'last_name': 'Smith', 'name': 'John'})
-
diff --git a/scrapy/trunk/docs/proposed/newitem.rst b/scrapy/trunk/docs/proposed/newitem.rst
new file mode 100644
index 000000000..fe15d3c04
--- /dev/null
+++ b/scrapy/trunk/docs/proposed/newitem.rst
@@ -0,0 +1,124 @@
+=====
+Items
+=====
+
+The goal of the scraping process is to obtain Items (aka Scraped Items) from
+scraped pages.
+
+An Item is a XXX
+
+Scrapy represent this using a model with fields for Items, much like you'll do
+in an ORM.
+
+Let's see an example::
+
+ class NewsItem(Item):
+ url = StringField()
+ headline = StringField()
+ summary = StringField()
+ content = StringField()
+ published = DateField()
+
+Using this may seen complicated at first, but gives you much power over scraped
+data, like assigning defaults for fields that are not present in some pages,
+performing validation, etc.
+
+To use Items you instantiate them and then assign values to their attributes,
+they will be converted to the expected Python types depending of the field
+kind::
+
+ ni = NewsItem()
+ ni.url = 'http://www.news.com/news/1'
+ ni.summary = 'Summary'
+ ni.content = 'Content'
+ ni.published = '2009-02-28'
+
+============
+ItemAdaptors
+============
+
+As you probably want to scrape the same kind of Items from many sources
+(different websites, RSS feeds, etc.), Scrapy implements ItemAdaptors, they
+allow you to adapt chunks of HTML or XML (selected using Selectors) to the
+expected format of your Item fields.
+
+An ItemAdaptor acts like a wrapper of an Item, you define an ItemAdaptor class,
+set the Item class to wrap and assign a set of functions (adaptor functions) to be called when you assign a value to a field.
+
+Here's an example of an ItemAdaptor for our previously created Item::
+
+ class NewsAdaptor(ItemAdaptor):
+ item_class = NewsItem
+
+ url = adaptor(extract, remove_tags(), unquote(), strip)
+ headline = adaptor(extract, remove_tags(), unquote(), strip)
+ summary = adaptor(extract, remove_tags(), unquote(), strip)
+ content = adaptor(extract, remove_tags(), unquote(), strip)
+
+How do we use it? Let's see it in action in a Spider::
+
+ def parse_newspage(self, response):
+ xhs = HtmlXPathSelector(response)
+ i = NewsAdaptor()
+
+ i.url = response.url
+ i.headline = xhs.x('//h1[@class="headline"]')
+ i.summary = xhs.x('//div[@class="summary"]')
+ i.content = xhs.x('//div[@id="body"]')
+ # we intentionally left publish out of the example, see below for site
+ # specific adaptors
+ return [i]
+
+What happens underneath?
+
+When we assign a value to a ItemAdaptor field it passes for the chain of
+functions defined previously in it's class, in this case, the value gets
+extracted (note that we assign directly the value obtained from the Selector),
+then tags will be removed, then the result will be unquoted, stripped and
+finally assigned to the Item Field.
+
+This final assignment is done in an internal instance of the Item on the
+ItemAdaptor, that's why we can return an ItemAdaptor instead of an Item and
+Scrapy will know how to extract the item from it.
+many sources and formats are you scraping from.
+
+A Item can have as many ItemAdaptors as you want it generally depends on how
+many sources and formats are you scraping from.
+
+ItemAdaptor inheritance
+=======================
+
+As we said before you generally want an ItemAdaptor for each different source of
+data and maybe some for specific sites, inheritance make this really easy, let's
+see an example of adapting HTML and XML::
+
+ class NewsAdaptor(ItemAdaptor):
+ item_class = NewsItem
+
+
+ class HtmlNewsAdaptor(NewsAdaptor):
+ url = adaptor(extract, remove_tags(), unquote(), strip)
+ headline = adaptor(extract, remove_tags(), unquote(), strip)
+ summary = adaptor(extract, remove_tags(), unquote(), strip)
+ content = adaptor(extract, remove_tags(), unquote(), strip)
+ published = adaptor(extract, remove_tags(), unquote(), strip)
+
+
+ class XmlNewsAdaptor(HtmlNewsAdaptor):
+ url = adaptor(extract, remove_root, strip)
+ headline = adaptor(extract, remove_root, strip)
+ summary = adaptor(extract, remove_root, strip)
+ content = adaptor(extract, remove_root, strip)
+ published = adaptor(extract, remove_root, strip)
+
+As the publication dates in news sites generally use different formats we would
+want one adaptor for a specific site or group of sites (that shares the same
+date format) this is easily accomplished::
+
+ class SpecificSiteNewsAdaptor(HtmlNewsAdaptor):
+ published = adaptor(NewsAdaptor.published, to_date('%d.%m.%Y'))
+
+This adds the adaptor function ``to_date`` to the end of our chain of adaptors.
+
+Note that ``SpecificSiteNewsAdaptor`` will inherit the field adaptations from
+``HtmlNewsAdaptor``.