2009-01-03 09:14:52 +00:00
|
|
|
.. _topics-selectors:
|
2008-11-27 15:44:29 +00:00
|
|
|
|
2012-09-13 15:24:44 -03:00
|
|
|
=========
|
|
|
|
Selectors
|
|
|
|
=========
|
2009-01-04 01:15:08 +00:00
|
|
|
|
2009-04-03 01:33:52 +00:00
|
|
|
When you're scraping web pages, the most common task you need to perform is
|
2009-04-10 05:35:53 +00:00
|
|
|
to extract data from the HTML source. There are several libraries available to
|
2012-09-30 14:55:55 -03:00
|
|
|
achieve this:
|
2008-11-27 15:44:29 +00:00
|
|
|
|
2009-04-03 01:33:52 +00:00
|
|
|
* `BeautifulSoup`_ is a very popular screen scraping library among Python
|
2012-09-30 14:55:55 -03:00
|
|
|
programmers which constructs a Python object based on the structure of the
|
|
|
|
HTML code and also deals with bad markup reasonably well, but it has one
|
|
|
|
drawback: it's slow.
|
2008-11-27 15:44:29 +00:00
|
|
|
|
2009-04-03 01:33:52 +00:00
|
|
|
* `lxml`_ is a XML parsing library (which also parses HTML) with a pythonic
|
|
|
|
API based on `ElementTree`_ (which is not part of the Python standard
|
|
|
|
library).
|
2008-11-27 15:44:29 +00:00
|
|
|
|
2012-09-30 14:55:55 -03:00
|
|
|
Scrapy comes with its own mechanism for extracting data. They're called
|
|
|
|
selectors because they "select" certain parts of the HTML document specified
|
|
|
|
either by `XPath`_ or `CSS`_ expressions.
|
2009-01-05 02:49:23 +00:00
|
|
|
|
2012-09-30 14:55:55 -03:00
|
|
|
`XPath`_ is a language for selecting nodes in XML documents, which can also be
|
|
|
|
used with HTML. `CSS`_ is a language for applying styles to HTML documents. It
|
|
|
|
defines selectors to associate those styles with specific HTML elements.
|
2008-11-27 15:44:29 +00:00
|
|
|
|
2013-09-24 12:34:46 -03:00
|
|
|
Scrapy selectors are built over the `lxml`_ library, which means they're very
|
|
|
|
similar in speed and parsing accuracy.
|
2008-11-27 15:44:29 +00:00
|
|
|
|
2009-04-03 01:33:52 +00:00
|
|
|
This page explains how selectors work and describes their API which is very
|
|
|
|
small and simple, unlike the `lxml`_ API which is much bigger because the
|
2010-08-21 01:26:35 -03:00
|
|
|
`lxml`_ library can be used for many other tasks, besides selecting markup
|
2009-04-03 01:33:52 +00:00
|
|
|
documents.
|
2008-11-27 15:44:29 +00:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
For a complete reference of the selectors API see
|
|
|
|
:ref:`Selector reference <topics-selectors-ref>`
|
2008-11-27 15:44:29 +00:00
|
|
|
|
2009-04-03 01:33:52 +00:00
|
|
|
.. _BeautifulSoup: http://www.crummy.com/software/BeautifulSoup/
|
|
|
|
.. _lxml: http://codespeak.net/lxml/
|
|
|
|
.. _ElementTree: http://docs.python.org/library/xml.etree.elementtree.html
|
2013-09-24 12:34:46 -03:00
|
|
|
.. _cssselect: https://pypi.python.org/pypi/cssselect/
|
2009-04-03 01:33:52 +00:00
|
|
|
.. _XPath: http://www.w3.org/TR/xpath
|
2012-09-30 14:55:55 -03:00
|
|
|
.. _CSS: http://www.w3.org/TR/selectors
|
2009-01-05 02:49:23 +00:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
|
2009-08-18 14:05:15 -03:00
|
|
|
Using selectors
|
|
|
|
===============
|
|
|
|
|
2009-04-03 01:33:52 +00:00
|
|
|
Constructing selectors
|
|
|
|
----------------------
|
2009-01-05 02:49:23 +00:00
|
|
|
|
2009-04-03 01:33:52 +00:00
|
|
|
.. highlight:: python
|
2008-11-27 15:44:29 +00:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
Scrapy selectors are instances of :class:`~scrapy.selector.Selector` class
|
|
|
|
constructed by passing a `Response` object as first argument, the response's
|
|
|
|
body is what they're going to be "selecting"::
|
|
|
|
|
2013-12-28 00:47:32 +06:00
|
|
|
from scrapy.spider import Spider
|
2013-10-14 16:31:20 -02:00
|
|
|
from scrapy.selector import Selector
|
2008-11-27 15:44:29 +00:00
|
|
|
|
2013-12-28 00:47:32 +06:00
|
|
|
class MySpider(Spider):
|
2013-10-14 16:31:20 -02:00
|
|
|
# ...
|
|
|
|
def parse(self, response):
|
2013-10-15 15:58:36 -02:00
|
|
|
sel = Selector(response)
|
2013-10-14 16:31:20 -02:00
|
|
|
# Using XPath query
|
2013-10-15 15:58:36 -02:00
|
|
|
print sel.xpath('//p')
|
2013-10-14 16:31:20 -02:00
|
|
|
# Using CSS query
|
2013-10-15 15:58:36 -02:00
|
|
|
print sel.css('p')
|
2013-10-14 16:31:20 -02:00
|
|
|
# Nesting queries
|
2013-10-15 15:58:36 -02:00
|
|
|
print sel.xpath('//div[@foo="bar"]').css('span#bold')
|
2009-04-03 01:33:52 +00:00
|
|
|
|
|
|
|
|
2012-09-30 14:55:55 -03:00
|
|
|
Using selectors
|
|
|
|
---------------
|
2009-04-03 01:33:52 +00:00
|
|
|
|
|
|
|
To explain how to use the selectors we'll use the `Scrapy shell` (which
|
2010-08-21 01:26:35 -03:00
|
|
|
provides interactive testing) and an example page located in the Scrapy
|
2009-04-03 01:33:52 +00:00
|
|
|
documentation server:
|
|
|
|
|
2012-06-24 01:00:33 -03:00
|
|
|
http://doc.scrapy.org/en/latest/_static/selectors-sample1.html
|
2009-04-03 01:33:52 +00:00
|
|
|
|
|
|
|
.. _topics-selectors-htmlcode:
|
|
|
|
|
|
|
|
Here's its HTML code:
|
|
|
|
|
|
|
|
.. literalinclude:: ../_static/selectors-sample1.html
|
|
|
|
:language: html
|
2009-01-05 02:49:23 +00:00
|
|
|
|
2009-04-03 01:33:52 +00:00
|
|
|
.. highlight:: sh
|
2008-11-27 15:44:29 +00:00
|
|
|
|
2009-04-03 01:33:52 +00:00
|
|
|
First, let's open the shell::
|
2008-11-27 15:44:29 +00:00
|
|
|
|
2012-06-24 01:00:33 -03:00
|
|
|
scrapy shell http://doc.scrapy.org/en/latest/_static/selectors-sample1.html
|
2008-11-27 15:44:29 +00:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
Then, after the shell loads, you'll have a selector already instantiated and
|
2013-10-15 15:58:36 -02:00
|
|
|
ready to use in ``sel`` shell variable.
|
2008-12-12 10:50:41 +00:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
Since we're dealing with HTML, the selector will automatically use an HTML parser.
|
2009-04-03 01:33:52 +00:00
|
|
|
|
|
|
|
.. highlight:: python
|
|
|
|
|
2012-09-30 14:55:55 -03:00
|
|
|
So, by looking at the :ref:`HTML code <topics-selectors-htmlcode>` of that
|
|
|
|
page, let's construct an XPath (using an HTML selector) for selecting the text
|
|
|
|
inside the title tag::
|
2008-11-27 15:44:29 +00:00
|
|
|
|
2013-10-15 15:58:36 -02:00
|
|
|
>>> sel.xpath('//title/text()')
|
2013-10-14 16:31:20 -02:00
|
|
|
[<Selector (text) xpath=//title/text()>]
|
2008-11-27 15:44:29 +00:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
As you can see, the ``.xpath()`` method returns an
|
|
|
|
:class:`~scrapy.selector.SelectorList` instance, which is a list of new
|
|
|
|
selectors. This API can be used quickly for extracting nested data.
|
2009-04-03 01:33:52 +00:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
To actually extract the textual data, you must call the selector ``.extract()``
|
2009-04-03 01:33:52 +00:00
|
|
|
method, as follows::
|
2008-11-27 15:44:29 +00:00
|
|
|
|
2013-10-15 15:58:36 -02:00
|
|
|
>>> sel.xpath('//title/text()').extract()
|
2009-01-05 02:49:23 +00:00
|
|
|
[u'Example website']
|
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
Notice that CSS selectors can select text or attribute nodes using CSS3
|
2013-09-24 12:34:46 -03:00
|
|
|
pseudo-elements::
|
2012-09-30 14:55:55 -03:00
|
|
|
|
2013-10-15 15:58:36 -02:00
|
|
|
>>> sel.css('title::text').extract()
|
2012-09-30 14:55:55 -03:00
|
|
|
[u'Example website']
|
|
|
|
|
2009-04-03 01:33:52 +00:00
|
|
|
Now we're going to get the base URL and some image links::
|
2009-01-05 02:49:23 +00:00
|
|
|
|
2013-10-15 15:58:36 -02:00
|
|
|
>>> sel.xpath('//base/@href').extract()
|
2009-01-05 02:49:23 +00:00
|
|
|
[u'http://example.com/']
|
|
|
|
|
2013-10-15 15:58:36 -02:00
|
|
|
>>> sel.css('base::attr(href)').extract()
|
2012-09-30 14:55:55 -03:00
|
|
|
[u'http://example.com/']
|
|
|
|
|
2013-10-15 15:58:36 -02:00
|
|
|
>>> sel.xpath('//a[contains(@href, "image")]/@href').extract()
|
2008-11-27 15:44:29 +00:00
|
|
|
[u'image1.html',
|
|
|
|
u'image2.html',
|
|
|
|
u'image3.html',
|
|
|
|
u'image4.html',
|
|
|
|
u'image5.html']
|
|
|
|
|
2013-10-15 15:58:36 -02:00
|
|
|
>>> sel.css('a[href*=image]::attr(href)').extract()
|
2012-09-30 14:55:55 -03:00
|
|
|
[u'image1.html',
|
|
|
|
u'image2.html',
|
|
|
|
u'image3.html',
|
|
|
|
u'image4.html',
|
|
|
|
u'image5.html']
|
|
|
|
|
2013-10-15 15:58:36 -02:00
|
|
|
>>> sel.xpath('//a[contains(@href, "image")]/img/@src').extract()
|
2008-11-27 15:44:29 +00:00
|
|
|
[u'image1_thumb.jpg',
|
|
|
|
u'image2_thumb.jpg',
|
|
|
|
u'image3_thumb.jpg',
|
|
|
|
u'image4_thumb.jpg',
|
|
|
|
u'image5_thumb.jpg']
|
|
|
|
|
2013-10-15 15:58:36 -02:00
|
|
|
>>> sel.css('a[href*=image] img::attr(src)').extract()
|
2012-09-30 14:55:55 -03:00
|
|
|
[u'image1_thumb.jpg',
|
|
|
|
u'image2_thumb.jpg',
|
|
|
|
u'image3_thumb.jpg',
|
|
|
|
u'image4_thumb.jpg',
|
|
|
|
u'image5_thumb.jpg']
|
2008-11-27 15:44:29 +00:00
|
|
|
|
2009-07-23 09:05:14 -03:00
|
|
|
.. _topics-selectors-nesting-selectors:
|
2008-11-27 15:44:29 +00:00
|
|
|
|
2009-04-03 01:33:52 +00:00
|
|
|
Nesting selectors
|
|
|
|
-----------------
|
2008-11-27 15:44:29 +00:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
The selection methods (``.xpath()`` or ``.css()``) returns a list of selectors
|
|
|
|
of the same type, so you can call the selection methods for those selectors
|
|
|
|
too. Here's an example::
|
2008-11-27 15:44:29 +00:00
|
|
|
|
2013-10-15 15:58:36 -02:00
|
|
|
>>> links = sel.xpath('//a[contains(@href, "image")]')
|
2009-01-05 02:49:23 +00:00
|
|
|
>>> links.extract()
|
2008-11-27 15:44:29 +00:00
|
|
|
[u'<a href="image1.html">Name: My image 1 <br><img src="image1_thumb.jpg"></a>',
|
|
|
|
u'<a href="image2.html">Name: My image 2 <br><img src="image2_thumb.jpg"></a>',
|
|
|
|
u'<a href="image3.html">Name: My image 3 <br><img src="image3_thumb.jpg"></a>',
|
|
|
|
u'<a href="image4.html">Name: My image 4 <br><img src="image4_thumb.jpg"></a>',
|
|
|
|
u'<a href="image5.html">Name: My image 5 <br><img src="image5_thumb.jpg"></a>']
|
|
|
|
|
2009-01-05 02:49:23 +00:00
|
|
|
>>> for index, link in enumerate(links):
|
2013-10-14 16:31:20 -02:00
|
|
|
args = (index, link.xpath('@href').extract(), link.xpath('img/@src').extract())
|
2009-04-03 01:33:52 +00:00
|
|
|
print 'Link number %d points to url %s and image %s' % args
|
2008-11-27 15:44:29 +00:00
|
|
|
|
|
|
|
Link number 0 points to url [u'image1.html'] and image [u'image1_thumb.jpg']
|
|
|
|
Link number 1 points to url [u'image2.html'] and image [u'image2_thumb.jpg']
|
|
|
|
Link number 2 points to url [u'image3.html'] and image [u'image3_thumb.jpg']
|
|
|
|
Link number 3 points to url [u'image4.html'] and image [u'image4_thumb.jpg']
|
|
|
|
Link number 4 points to url [u'image5.html'] and image [u'image5_thumb.jpg']
|
|
|
|
|
2012-09-30 14:55:55 -03:00
|
|
|
Using selectors with regular expressions
|
|
|
|
----------------------------------------
|
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
:class:`~scrapy.selector.Selector` also have a ``.re()`` method for extracting
|
|
|
|
data using regular expressions. However, unlike using ``.xpath()`` or
|
|
|
|
``.css()`` methods, ``.re()`` method returns a list of unicode strings. So you
|
|
|
|
can't construct nested ``.re()`` calls.
|
2012-09-30 14:55:55 -03:00
|
|
|
|
|
|
|
Here's an example used to extract images names from the :ref:`HTML code
|
|
|
|
<topics-selectors-htmlcode>` above::
|
|
|
|
|
2013-10-15 15:58:36 -02:00
|
|
|
>>> sel.xpath('//a[contains(@href, "image")]/text()').re(r'Name:\s*(.*)')
|
2012-09-30 14:55:55 -03:00
|
|
|
[u'My image 1',
|
|
|
|
u'My image 2',
|
|
|
|
u'My image 3',
|
|
|
|
u'My image 4',
|
|
|
|
u'My image 5']
|
|
|
|
|
2009-07-23 09:05:14 -03:00
|
|
|
.. _topics-selectors-relative-xpaths:
|
|
|
|
|
2009-07-16 17:29:29 -03:00
|
|
|
Working with relative XPaths
|
|
|
|
----------------------------
|
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
Keep in mind that if you are nesting selectors and use an XPath that starts
|
|
|
|
with ``/``, that XPath will be absolute to the document and not relative to the
|
|
|
|
``Selector`` you're calling it from.
|
2009-07-16 17:29:29 -03:00
|
|
|
|
|
|
|
For example, suppose you want to extract all ``<p>`` elements inside ``<div>``
|
2010-08-21 01:26:35 -03:00
|
|
|
elements. First, you would get all ``<div>`` elements::
|
2009-07-16 17:29:29 -03:00
|
|
|
|
2013-10-15 15:58:36 -02:00
|
|
|
>>> divs = sel.xpath('//div')
|
2009-07-16 17:29:29 -03:00
|
|
|
|
|
|
|
At first, you may be tempted to use the following approach, which is wrong, as
|
|
|
|
it actually extracts all ``<p>`` elements from the document, not only those
|
|
|
|
inside ``<div>`` elements::
|
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
>>> for p in divs.xpath('//p') # this is wrong - gets all <p> from the whole document
|
2009-07-16 17:29:29 -03:00
|
|
|
>>> print p.extract()
|
|
|
|
|
|
|
|
This is the proper way to do it (note the dot prefixing the ``.//p`` XPath)::
|
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
>>> for p in divs.xpath('.//p') # extracts all <p> inside
|
2009-07-16 17:29:29 -03:00
|
|
|
>>> print p.extract()
|
|
|
|
|
|
|
|
Another common case would be to extract all direct ``<p>`` children::
|
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
>>> for p in divs.xpath('p')
|
2009-07-16 17:29:29 -03:00
|
|
|
>>> print p.extract()
|
|
|
|
|
|
|
|
For more details about relative XPaths see the `Location Paths`_ section in the
|
|
|
|
XPath specification.
|
|
|
|
|
|
|
|
.. _Location Paths: http://www.w3.org/TR/xpath#location-paths
|
2009-08-18 14:05:15 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. _topics-selectors-ref:
|
|
|
|
|
2012-09-30 14:55:55 -03:00
|
|
|
Built-in Selectors reference
|
2013-09-24 12:34:46 -03:00
|
|
|
============================
|
2009-08-18 14:05:15 -03:00
|
|
|
|
2009-08-19 21:50:52 -03:00
|
|
|
.. module:: scrapy.selector
|
2013-10-14 16:31:20 -02:00
|
|
|
:synopsis: Selector class
|
2013-09-24 12:34:46 -03:00
|
|
|
|
2013-10-16 17:37:22 -02:00
|
|
|
.. class:: Selector(response=None, text=None, type=None)
|
2013-09-24 12:34:46 -03:00
|
|
|
|
2013-10-16 17:37:22 -02:00
|
|
|
An instance of :class:`Selector` is a wrapper over response to select
|
2013-10-14 16:31:20 -02:00
|
|
|
certain parts of its content.
|
2013-09-24 12:34:46 -03:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
``response`` is a :class:`~scrapy.http.HtmlResponse` or
|
|
|
|
:class:`~scrapy.http.XmlResponse` object that will be used for selecting and
|
|
|
|
extracting data.
|
2013-09-24 12:34:46 -03:00
|
|
|
|
2013-10-16 17:37:22 -02:00
|
|
|
``text`` is a unicode string or utf-8 encoded text for cases when a
|
|
|
|
``response`` isn't available. Using ``text`` and ``response`` together is
|
|
|
|
undefined behavior.
|
|
|
|
|
|
|
|
``type`` defines the selector type, it can be ``"html"``, ``"xml"`` or ``None`` (default).
|
|
|
|
|
|
|
|
If ``type`` is ``None``, the selector automatically chooses the best type
|
|
|
|
based on ``response`` type (see below), or defaults to ``"html"`` in case it
|
|
|
|
is used together with ``text``.
|
|
|
|
|
|
|
|
If ``type`` is ``None`` and a ``response`` is passed, the selector type is
|
|
|
|
inferred from the response type as follow:
|
|
|
|
|
|
|
|
* ``"html"`` for :class:`~scrapy.http.HtmlResponse` type
|
|
|
|
* ``"xml"`` for :class:`~scrapy.http.XmlResponse` type
|
|
|
|
* ``"html"`` for anything else
|
|
|
|
|
|
|
|
Otherwise, if ``type`` is set, the selector type will be forced and no
|
|
|
|
detection will occur.
|
2013-09-24 12:34:46 -03:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
.. method:: xpath(query)
|
2013-09-24 12:34:46 -03:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
Find nodes matching the xpath ``query`` and return the result as a
|
|
|
|
:class:`SelectorList` instance with all elements flattened. List
|
|
|
|
elements implement :class:`Selector` interface too.
|
2013-09-24 12:34:46 -03:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
``query`` is a string containing the XPATH query to apply.
|
2013-09-24 12:34:46 -03:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
.. method:: css(query)
|
2013-09-24 12:34:46 -03:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
Apply the given CSS selector and return a :class:`SelectorList` instance.
|
2013-09-24 12:34:46 -03:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
``query`` is a string containing the CSS selector to apply.
|
2013-09-24 12:34:46 -03:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
In the background, CSS queries are translated into XPath queries using
|
|
|
|
`cssselect`_ library and run ``.xpath()`` method.
|
2013-09-24 12:34:46 -03:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
.. method:: extract()
|
2013-09-24 12:34:46 -03:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
Serialize and return the matched nodes as a list of unicode strings.
|
|
|
|
Percent encoded content is unquoted.
|
2013-09-24 12:34:46 -03:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
.. method:: re(regex)
|
2013-09-24 12:34:46 -03:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
Apply the given regex and return a list of unicode strings with the
|
|
|
|
matches.
|
2013-09-24 12:34:46 -03:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
``regex`` can be either a compiled regular expression or a string which
|
|
|
|
will be compiled to a regular expression using ``re.compile(regex)``
|
2013-09-24 12:34:46 -03:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
.. method:: register_namespace(prefix, uri)
|
2013-09-24 12:34:46 -03:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
Register the given namespace to be used in this :class:`Selector`.
|
|
|
|
Without registering namespaces you can't select or extract data from
|
|
|
|
non-standard namespaces. See examples below.
|
2013-09-24 12:34:46 -03:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
.. method:: remove_namespaces()
|
2013-09-24 12:34:46 -03:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
Remove all namespaces, allowing to traverse the document using
|
|
|
|
namespace-less xpaths. See example below.
|
2009-08-18 14:05:15 -03:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
.. method:: __nonzero__()
|
2009-08-18 14:05:15 -03:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
Returns ``True`` if there is any real content selected or ``False``
|
|
|
|
otherwise. In other words, the boolean value of a :class:`Selector` is
|
|
|
|
given by the contents it selects.
|
2009-08-18 14:05:15 -03:00
|
|
|
|
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
SelectorList objects
|
|
|
|
--------------------
|
2009-08-18 15:13:23 -03:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
.. class:: SelectorList
|
2009-08-18 15:13:23 -03:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
The :class:`SelectorList` class is subclass of the builtin ``list``
|
|
|
|
class, which provides a few additional methods.
|
2013-09-24 12:34:46 -03:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
.. method:: xpath(query)
|
2009-08-18 14:05:15 -03:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
Call the ``.xpath()`` method for each element in this list and return
|
|
|
|
their results flattened as another :class:`SelectorList`.
|
2009-08-18 14:05:15 -03:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
``query`` is the same argument as the one in :meth:`Selector.xpath`
|
2009-08-18 14:05:15 -03:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
.. method:: css(query)
|
2009-08-18 14:05:15 -03:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
Call the ``.css()`` method for each element in this list and return
|
|
|
|
their results flattened as another :class:`SelectorList`.
|
2009-08-18 14:05:15 -03:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
``query`` is the same argument as the one in :meth:`Selector.css`
|
2009-08-18 14:05:15 -03:00
|
|
|
|
2010-08-21 01:26:35 -03:00
|
|
|
.. method:: extract()
|
2009-08-18 14:05:15 -03:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
Call the ``.extract()`` method for each element is this list and return
|
|
|
|
their results flattened, as a list of unicode strings.
|
2013-01-18 12:19:58 -02:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
.. method:: re()
|
2013-01-18 12:19:58 -02:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
Call the ``.re()`` method for each element is this list and return
|
|
|
|
their results flattened, as a list of unicode strings.
|
2009-08-18 14:05:15 -03:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
.. method:: __nonzero__()
|
2009-08-18 14:05:15 -03:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
returns True if the list is not empty, False otherwise.
|
2009-08-18 14:05:15 -03:00
|
|
|
|
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
Selector examples on HTML response
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2009-08-18 14:05:15 -03:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
Here's a couple of :class:`Selector` examples to illustrate several concepts.
|
|
|
|
In all cases, we assume there is already an :class:`Selector` instantiated with
|
|
|
|
a :class:`~scrapy.http.HtmlResponse` object like this::
|
2009-08-18 14:05:15 -03:00
|
|
|
|
2013-10-16 22:34:08 -02:00
|
|
|
sel = Selector(html_response)
|
2009-08-18 14:05:15 -03:00
|
|
|
|
|
|
|
1. Select all ``<h1>`` elements from a HTML response body, returning a list of
|
2013-10-14 16:31:20 -02:00
|
|
|
:class:`Selector` objects (ie. a :class:`SelectorList` object)::
|
2009-08-18 14:05:15 -03:00
|
|
|
|
2013-10-16 22:34:08 -02:00
|
|
|
sel.xpath("//h1")
|
2009-08-18 14:05:15 -03:00
|
|
|
|
|
|
|
2. Extract the text of all ``<h1>`` elements from a HTML response body,
|
|
|
|
returning a list of unicode strings::
|
|
|
|
|
2013-10-16 22:34:08 -02:00
|
|
|
sel.xpath("//h1").extract() # this includes the h1 tag
|
|
|
|
sel.xpath("//h1/text()").extract() # this excludes the h1 tag
|
2009-08-18 14:05:15 -03:00
|
|
|
|
|
|
|
3. Iterate over all ``<p>`` tags and print their class attribute::
|
|
|
|
|
2013-10-16 22:34:08 -02:00
|
|
|
for node in sel.xpath("//p"):
|
2013-10-14 16:31:20 -02:00
|
|
|
... print node.xpath("@class").extract()
|
2009-08-18 14:05:15 -03:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
Selector examples on XML response
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2009-08-18 14:05:15 -03:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
Here's a couple of examples to illustrate several concepts. In both cases we
|
|
|
|
assume there is already an :class:`Selector` instantiated with a
|
|
|
|
:class:`~scrapy.http.XmlResponse` object like this::
|
2009-08-18 14:05:15 -03:00
|
|
|
|
2013-10-16 22:34:08 -02:00
|
|
|
sel = Selector(xml_response)
|
2009-08-18 14:05:15 -03:00
|
|
|
|
2012-09-30 14:55:55 -03:00
|
|
|
1. Select all ``<product>`` elements from a XML response body, returning a list
|
2013-10-14 16:31:20 -02:00
|
|
|
of :class:`Selector` objects (ie. a :class:`SelectorList` object)::
|
2009-08-18 14:05:15 -03:00
|
|
|
|
2013-10-16 22:34:08 -02:00
|
|
|
sel.xpath("//product")
|
2009-08-18 14:05:15 -03:00
|
|
|
|
|
|
|
2. Extract all prices from a `Google Base XML feed`_ which requires registering
|
|
|
|
a namespace::
|
|
|
|
|
2013-10-16 22:34:08 -02:00
|
|
|
sel.register_namespace("g", "http://base.google.com/ns/1.0")
|
|
|
|
sel.xpath("//g:price").extract()
|
2009-08-18 14:05:15 -03:00
|
|
|
|
2013-01-18 12:35:30 -02:00
|
|
|
.. _removing-namespaces:
|
|
|
|
|
2013-01-18 12:19:58 -02:00
|
|
|
Removing namespaces
|
|
|
|
~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
When dealing with scraping projects, it is often quite convenient to get rid of
|
2013-01-18 12:35:30 -02:00
|
|
|
namespaces altogether and just work with element names, to write more
|
|
|
|
simple/convenient XPaths. You can use the
|
2013-10-14 16:31:20 -02:00
|
|
|
:meth:`Selector.remove_namespaces` method for that.
|
2013-01-18 12:35:30 -02:00
|
|
|
|
|
|
|
Let's show an example that illustrates this with Github blog atom feed.
|
2013-01-18 12:19:58 -02:00
|
|
|
|
2013-01-18 12:35:30 -02:00
|
|
|
First, we open the shell with the url we want to scrape::
|
2013-01-18 12:19:58 -02:00
|
|
|
|
|
|
|
$ scrapy shell https://github.com/blog.atom
|
2013-01-18 12:35:30 -02:00
|
|
|
|
|
|
|
Once in the shell we can try selecting all ``<link>`` objects and see that it
|
|
|
|
doesn't work (because the Atom XML namespace is obfuscating those nodes)::
|
|
|
|
|
2013-10-16 22:34:08 -02:00
|
|
|
>>> sel.xpath("//link")
|
2013-01-18 12:19:58 -02:00
|
|
|
[]
|
2013-01-18 12:35:30 -02:00
|
|
|
|
2013-10-14 16:31:20 -02:00
|
|
|
But once we call the :meth:`Selector.remove_namespaces` method, all
|
2013-01-18 12:35:30 -02:00
|
|
|
nodes can be accessed directly by their names::
|
|
|
|
|
2013-10-16 22:34:08 -02:00
|
|
|
>>> sel.remove_namespaces()
|
|
|
|
>>> sel.xpath("//link")
|
2013-10-14 16:31:20 -02:00
|
|
|
[<Selector xpath='//link' data=u'<link xmlns="http://www.w3.org/2005/Atom'>,
|
|
|
|
<Selector xpath='//link' data=u'<link xmlns="http://www.w3.org/2005/Atom'>,
|
2013-01-18 12:19:58 -02:00
|
|
|
...
|
2013-01-18 12:35:30 -02:00
|
|
|
|
|
|
|
If you wonder why the namespace removal procedure is not always called, instead
|
|
|
|
of having to call it manually. This is because of two reasons which, in order
|
|
|
|
of relevance, are:
|
|
|
|
|
2013-09-24 12:34:46 -03:00
|
|
|
1. Removing namespaces requires to iterate and modify all nodes in the
|
2013-01-18 12:35:30 -02:00
|
|
|
document, which is a reasonably expensive operation to performs for all
|
|
|
|
documents crawled by Scrapy
|
|
|
|
|
2013-09-24 12:34:46 -03:00
|
|
|
2. There could be some cases where using namespaces is actually required, in
|
2013-01-18 12:35:30 -02:00
|
|
|
case some element names clash between namespaces. These cases are very rare
|
|
|
|
though.
|
2013-01-18 12:19:58 -02:00
|
|
|
|
2009-08-18 14:05:15 -03:00
|
|
|
.. _Google Base XML feed: http://base.google.com/support/bin/answer.py?hl=en&answer=59461
|