2009-01-03 09:14:52 +00:00
|
|
|
.. _topics-selectors:
|
2008-11-27 15:44:29 +00:00
|
|
|
|
2009-04-10 05:35:53 +00:00
|
|
|
===============
|
|
|
|
XPath 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
|
2009-04-03 01:33:52 +00: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
|
|
|
|
programmers which constructs a Python object based on the
|
|
|
|
structure of the HTML code and also deals with bad markup reasonable 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
|
|
|
|
2009-04-10 05:35:53 +00:00
|
|
|
Scrapy comes with its own mechanism for extracting data. They're called XPath
|
|
|
|
selectors (or just "selectors", for short) because they "select" certain parts
|
|
|
|
of the HTML document specified by `XPath`_ expressions.
|
2009-01-05 02:49:23 +00:00
|
|
|
|
2009-04-10 05:35:53 +00:00
|
|
|
`XPath`_ is a language for selecting nodes in XML documents, which can be used
|
|
|
|
to with HTML.
|
2008-11-27 15:44:29 +00:00
|
|
|
|
2009-04-03 01:33:52 +00:00
|
|
|
Both `lxml`_ and Scrapy Selectors are built over the `libxml2`_ 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
|
|
|
|
`lxml`_ library can be use for many other tasks, besides selecting markup
|
|
|
|
documents.
|
2008-11-27 15:44:29 +00:00
|
|
|
|
2009-04-03 01:33:52 +00:00
|
|
|
For a complete reference of the selectors API see the :ref:`XPath selector
|
2009-08-18 14:05:15 -03:00
|
|
|
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
|
|
|
|
.. _libxml2: http://xmlsoft.org/
|
|
|
|
.. _XPath: http://www.w3.org/TR/xpath
|
2009-01-05 02:49:23 +00: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
|
|
|
There are two types of selectors bundled with Scrapy. Those are:
|
2009-01-05 02:49:23 +00:00
|
|
|
|
2009-04-03 01:33:52 +00:00
|
|
|
* :class:`~scrapy.xpath.HtmlXPathSelector` - for working with HTML documents
|
2008-11-27 15:44:29 +00:00
|
|
|
|
2009-04-03 01:33:52 +00:00
|
|
|
* :class:`~scrapy.xpath.XmlXPathSelector` - for working with XML documents
|
2008-11-27 15:44:29 +00:00
|
|
|
|
2009-04-03 01:33:52 +00:00
|
|
|
.. highlight:: python
|
2008-11-27 15:44:29 +00:00
|
|
|
|
2009-04-03 01:33:52 +00:00
|
|
|
Both share the same selector API, and are constructed with a Response object as
|
|
|
|
its first parameter. This is the Response they're gonna be "selecting".
|
2008-11-27 15:44:29 +00:00
|
|
|
|
2009-04-03 01:33:52 +00:00
|
|
|
Example::
|
|
|
|
|
|
|
|
hxs = HtmlXPathSelector(response) # a HTML selector
|
|
|
|
xxs = XmlXPathSelector(response) # a XML selector
|
|
|
|
|
|
|
|
Using selectors with XPaths
|
|
|
|
---------------------------
|
|
|
|
|
|
|
|
To explain how to use the selectors we'll use the `Scrapy shell` (which
|
|
|
|
provides interactive testing) and an example page located in Scrapy
|
|
|
|
documentation server:
|
|
|
|
|
|
|
|
http://doc.scrapy.org/_static/selectors-sample1.html
|
|
|
|
|
|
|
|
.. _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
|
|
|
|
2009-04-03 01:33:52 +00:00
|
|
|
scrapy-ctl.py shell http://doc.scrapy.org/_static/selectors-sample1.html
|
2008-11-27 15:44:29 +00:00
|
|
|
|
2009-04-03 01:33:52 +00:00
|
|
|
Then, after the shell loads, you'll have some selectors already instanced and
|
|
|
|
ready to use.
|
2008-12-12 10:50:41 +00:00
|
|
|
|
2009-04-03 01:33:52 +00:00
|
|
|
Since we're dealing with HTML we'll be using the
|
|
|
|
:class:`~scrapy.xpath.HtmlXPathSelector` object which is found, by default, in
|
|
|
|
the ``hxs`` shell variable.
|
|
|
|
|
|
|
|
.. highlight:: python
|
|
|
|
|
|
|
|
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
|
|
|
|
2009-08-17 15:58:06 -03:00
|
|
|
>>> hxs.select('//title/text()')
|
2009-01-05 02:49:23 +00:00
|
|
|
[<HtmlXPathSelector (text) xpath=//title/text()>]
|
2008-11-27 15:44:29 +00:00
|
|
|
|
2009-08-17 15:58:06 -03:00
|
|
|
As you can see, the select() method returns a XPathSelectorList, which is a list of
|
2009-04-03 01:33:52 +00:00
|
|
|
new selectors. This API can be used quickly for extracting nested data.
|
|
|
|
|
|
|
|
To actually extract the textual data you must call the selector ``extract()``
|
|
|
|
method, as follows::
|
2008-11-27 15:44:29 +00:00
|
|
|
|
2009-08-17 15:58:06 -03:00
|
|
|
>>> hxs.select('//title/text()').extract()
|
2009-01-05 02:49:23 +00: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
|
|
|
|
2009-08-17 15:58:06 -03:00
|
|
|
>>> hxs.select('//base/@href').extract()
|
2009-01-05 02:49:23 +00:00
|
|
|
[u'http://example.com/']
|
|
|
|
|
2009-08-17 15:58:06 -03:00
|
|
|
>>> hxs.select('//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']
|
|
|
|
|
2009-08-17 15:58:06 -03:00
|
|
|
>>> hxs.select('//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']
|
|
|
|
|
2009-04-03 01:33:52 +00:00
|
|
|
|
|
|
|
Using selectors with regular expressions
|
|
|
|
----------------------------------------
|
|
|
|
|
|
|
|
Selectors also have a ``re()`` method for extracting data using regular
|
2009-08-17 15:58:06 -03:00
|
|
|
expressions. However, unlike using the ``select()`` method, the ``re()`` method
|
|
|
|
does not return a list of :class:`~scrapy.xpath.XPathSelector` objects, so you
|
|
|
|
can't construct nested ``.re()`` calls.
|
2009-04-03 01:33:52 +00:00
|
|
|
|
|
|
|
Here's an example used to extract images names from the :ref:`HTML code
|
|
|
|
<topics-selectors-htmlcode>` above::
|
2009-01-05 02:49:23 +00:00
|
|
|
|
2009-08-17 15:58:06 -03:00
|
|
|
>>> hxs.select('//a[contains(@href, "image")]/text()').re(r'Name:\s*(.*)')
|
2008-11-27 15:44:29 +00: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-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
|
|
|
|
2009-08-17 15:58:06 -03:00
|
|
|
The ``select()`` selector method returns a list of selectors, so you can call the
|
|
|
|
``select()`` for those selectors too. Here's an example::
|
2008-11-27 15:44:29 +00:00
|
|
|
|
2009-08-17 15:58:06 -03:00
|
|
|
>>> links = hxs.select('//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):
|
2009-08-17 15:58:06 -03:00
|
|
|
args = (index, link.select('@href').extract(), link.select('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']
|
|
|
|
|
2009-07-23 09:05:14 -03:00
|
|
|
.. _topics-selectors-relative-xpaths:
|
|
|
|
|
2009-07-16 17:29:29 -03:00
|
|
|
Working with relative XPaths
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
Keep in mind that if you are nesting XPathSelectors and use an XPath that
|
|
|
|
starts with ``/``, that XPath will be absolute to the document and not relative
|
|
|
|
to the ``XPathSelector`` you're calling it from.
|
|
|
|
|
|
|
|
For example, suppose you want to extract all ``<p>`` elements inside ``<div>``
|
|
|
|
elements. First you get would get all ``<div>`` elements::
|
|
|
|
|
2009-08-17 15:58:06 -03:00
|
|
|
>>> divs = hxs.select('//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::
|
|
|
|
|
2009-08-17 15:58:06 -03:00
|
|
|
>>> for p in divs.select('//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)::
|
|
|
|
|
2009-08-17 15:58:06 -03:00
|
|
|
>>> for p in divs.select('.//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::
|
|
|
|
|
2009-08-17 15:58:06 -03:00
|
|
|
>>> for p in divs.select('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:
|
|
|
|
|
|
|
|
Built-in XPath Selectors reference
|
|
|
|
==================================
|
|
|
|
|
|
|
|
.. module:: scrapy.xpath
|
|
|
|
:synopsis: XPath selectors classes
|
|
|
|
|
|
|
|
There are two types of selectors bundled with Scrapy:
|
|
|
|
:class:`HtmlXPathSelector` and :class:`XmlXPathSelector`. Both of them
|
|
|
|
implement the same :class:`XPathSelector` interface. The only different is that
|
|
|
|
one is used to process HTML data and the other XML data.
|
|
|
|
|
|
|
|
XPathSelector objects
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
.. class:: XPathSelector(response)
|
|
|
|
|
|
|
|
A :class:`XPathSelector` object is a wrapper over response to select
|
|
|
|
certain parts of its content.
|
|
|
|
|
|
|
|
A :class:`Request` object represents an HTTP request, which is usually
|
|
|
|
generated in the Spider and executed by the Downloader, and thus generating
|
|
|
|
a :class:`Response`.
|
|
|
|
|
|
|
|
``url`` is a :class:`~scrapy.http.Response` object that will be used for
|
|
|
|
selecting and extracting data
|
|
|
|
|
|
|
|
.. method:: XPathSelector.select(xpath)
|
|
|
|
|
|
|
|
Apply the given XPath relative to this XPathSelector and return a list
|
|
|
|
of :class:`XPathSelector` objects (ie. a :class:`XPathSelectorList`) with
|
|
|
|
the result.
|
|
|
|
|
|
|
|
``xpath`` is a string containing the XPath to apply
|
|
|
|
|
|
|
|
.. method:: XPathSelector.re(regex)
|
|
|
|
|
|
|
|
Apply the given regex and return a list of unicode strings with the
|
|
|
|
matches.
|
|
|
|
|
|
|
|
``regex`` can be either a compiled regular expression or a string which
|
|
|
|
will be compiled to a regular expression using ``re.compile(regex)``
|
|
|
|
|
|
|
|
.. method:: XPathSelector.extract()
|
|
|
|
|
|
|
|
Return a unicode string with the content of this :class:`XPathSelector`
|
|
|
|
object.
|
|
|
|
|
|
|
|
.. method:: XPathSelector.extract_unquoted()
|
|
|
|
|
|
|
|
Return a unicode string with the content of this :class:`XPathSelector`
|
|
|
|
without entities or CDATA. This method is intended to be use for text-only
|
|
|
|
selectors, like ``//h1/text()`` (but not ``//h1``). If it's used for
|
|
|
|
:class:`XPathSelector` objects which don't select a textual content (ie. if
|
|
|
|
they contain tags), the output of this method is undefined.
|
|
|
|
|
|
|
|
.. method:: XPathSelector.register_namespace(prefix, uri)
|
|
|
|
|
|
|
|
Register the given namespace to be used in this :class:`XPathSelector`.
|
|
|
|
Without registering namespaces you can't select or extract data from
|
|
|
|
non-standard namespaces. See examples below.
|
|
|
|
|
|
|
|
.. method:: XPathSelector.__nonzero__()
|
|
|
|
|
|
|
|
Returns ``True`` if there is any real content selected by this
|
|
|
|
:class:`XPathSelector` or ``False`` otherwise. In other words, the boolean
|
|
|
|
value of an XPathSelector is given by the contents it selects.
|
|
|
|
|
|
|
|
XPathSelectorList objects
|
|
|
|
-------------------------
|
|
|
|
|
|
|
|
.. class:: XPathSelectorList
|
|
|
|
|
|
|
|
The :class:`XPathSelectorList` class is subclass of the builtin ``list``
|
|
|
|
class, which provides a few additional methods.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: XPathSelectorList.select(xpath)
|
|
|
|
|
|
|
|
Call the :meth:`XPathSelector.re` method for all :class:`XPathSelector`
|
|
|
|
objects in this list and return their results flattened, as new
|
|
|
|
:class:`XPathSelectorList`.
|
|
|
|
|
|
|
|
``xpath`` is the same argument as the one in :meth:`XPathSelector.x`
|
|
|
|
|
|
|
|
.. method:: XPathSelector.re(regex)
|
|
|
|
|
|
|
|
Call the :meth:`XPathSelector.re` method for all :class:`XPathSelector`
|
|
|
|
objects in this list and return their results flattened, as a list of
|
|
|
|
unicode strings.
|
|
|
|
|
|
|
|
``regex`` is the same argument as the one in :meth:`XPathSelector.re`
|
|
|
|
|
|
|
|
.. method:: XPathSelector.extract()
|
|
|
|
|
|
|
|
Call the :meth:`XPathSelector.re` method for all :class:`XPathSelector`
|
|
|
|
objects in this list and return their results flattened, as a list of
|
|
|
|
unicode strings.
|
|
|
|
|
|
|
|
.. method:: XPathSelector.extract_unquoted()
|
|
|
|
|
|
|
|
Call the :meth:`XPathSelector.extract_unoquoted` method for all
|
|
|
|
:class:`XPathSelector` objects in this list and return their results
|
|
|
|
flattened, as a list of unicode strings. This method should not be applied
|
|
|
|
to all kinds of XPathSelectors. For more info see
|
|
|
|
:meth:`XPathSelector.extract_unoquoted`.
|
|
|
|
|
|
|
|
HtmlXPathSelector objects
|
|
|
|
-------------------------
|
|
|
|
|
|
|
|
.. class:: HtmlXPathSelector(response)
|
|
|
|
|
|
|
|
A subclass of :class:`XPathSelector` for working with HTML content. It uses
|
|
|
|
the `libxml2`_ HTML parser. See the :class:`XPathSelector` API for more info.
|
|
|
|
|
|
|
|
.. _libxml2: http://xmlsoft.org/
|
|
|
|
|
|
|
|
HtmlXPathSelector examples
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Here's a couple of :class:`HtmlXPathSelector` examples to illustrate several
|
|
|
|
concepts. In all cases we assume there is already a :class:`HtmlPathSelector`
|
|
|
|
instanced with a :class:`~scrapy.http.Response` object like this::
|
|
|
|
|
|
|
|
x = HtmlXPathSelector(html_response)
|
|
|
|
|
|
|
|
1. Select all ``<h1>`` elements from a HTML response body, returning a list of
|
|
|
|
:class:`XPathSelector` objects (ie. a :class:`XPathSelectorList` object)::
|
|
|
|
|
|
|
|
x.select("//h1")
|
|
|
|
|
|
|
|
2. Extract the text of all ``<h1>`` elements from a HTML response body,
|
|
|
|
returning a list of unicode strings::
|
|
|
|
|
|
|
|
x.select("//h1").extract() # this includes the h1 tag
|
|
|
|
x.select("//h1/text()").extract() # this excludes the h1 tag
|
|
|
|
|
|
|
|
3. Iterate over all ``<p>`` tags and print their class attribute::
|
|
|
|
|
|
|
|
for node in x.select("//p"):
|
|
|
|
... print node.select("@href")
|
|
|
|
|
|
|
|
4. Extract textual data from all ``<p>`` tags without entities, as a list of
|
|
|
|
unicode strings::
|
|
|
|
|
|
|
|
x.select("//p/text()").extract_unquoted()
|
|
|
|
|
|
|
|
# the following line is wrong. extract_unquoted() should only be used
|
|
|
|
# with textual XPathSelectors
|
|
|
|
x.select("//p").extract_unquoted() # it may work but output is unpredictable
|
|
|
|
|
|
|
|
XmlXPathSelector objects
|
|
|
|
------------------------
|
|
|
|
|
|
|
|
.. class:: XmlXPathSelector(response)
|
|
|
|
|
|
|
|
A subclass of :class:`XPathSelector` for working with XML content. It uses
|
|
|
|
the `libxml2`_ XML parser. See the :class:`XPathSelector` API for more info.
|
|
|
|
|
|
|
|
XmlXPathSelector examples
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Here's a couple of :class:`XmlXPathSelector` examples to illustrate several
|
|
|
|
concepts. In all cases we assume there is already a :class:`XmlPathSelector`
|
|
|
|
instanced with a :class:`~scrapy.http.Response` object like this::
|
|
|
|
|
|
|
|
x = HtmlXPathSelector(xml_response)
|
|
|
|
|
|
|
|
1. Select all ``<product>`` elements from a XML response body, returning a list of
|
|
|
|
:class:`XPathSelector` objects (ie. a :class:`XPathSelectorList` object)::
|
|
|
|
|
|
|
|
x.select("//h1")
|
|
|
|
|
|
|
|
2. Extract all prices from a `Google Base XML feed`_ which requires registering
|
|
|
|
a namespace::
|
|
|
|
|
|
|
|
x.register_namespace("g", "http://base.google.com/ns/1.0")
|
|
|
|
x.select("//g:price").extract()
|
|
|
|
|
|
|
|
.. _Google Base XML feed: http://base.google.com/support/bin/answer.py?hl=en&answer=59461
|