diff --git a/scrapy/trunk/docs/ref/index.rst b/scrapy/trunk/docs/ref/index.rst index 880231f65..0a5746f05 100644 --- a/scrapy/trunk/docs/ref/index.rst +++ b/scrapy/trunk/docs/ref/index.rst @@ -9,6 +9,7 @@ This section documents the API of Scrapy |version|. For more information see :re :maxdepth: 1 spiders + selectors exceptions request-response extension-manager diff --git a/scrapy/trunk/docs/ref/request-response.rst b/scrapy/trunk/docs/ref/request-response.rst index 591742f9c..bbb4ec9af 100644 --- a/scrapy/trunk/docs/ref/request-response.rst +++ b/scrapy/trunk/docs/ref/request-response.rst @@ -75,8 +75,10 @@ Request objects be filtered by the scheduler. This is used when you want to perform an identical request multiple times, for whatever reason - ``errback`` is a function that will be called if any exception was raised while - processing the request, it takes a `Twisted Failure`_ instance as first parameter. + ``errback`` is a function that will be called if any exception was raised + while processing the request in Scrapy. This includes pages that failed + with 404 HTTP errors and such. , it receives a `Twisted Failure`_ + instance as first parameter. .. _Twisted Failure: http://twistedmatrix.com/documents/8.2.0/api/twisted.python.failure.Failure.html diff --git a/scrapy/trunk/docs/ref/selectors.rst b/scrapy/trunk/docs/ref/selectors.rst new file mode 100644 index 000000000..0ee82f687 --- /dev/null +++ b/scrapy/trunk/docs/ref/selectors.rst @@ -0,0 +1,186 @@ +.. _ref-selectors: + +============= +Selectors API +============= + +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 + + +XPathSelector Methods +--------------------- + +.. method:: XPathSelector.x(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. + + +XPathSelectorList Methods +------------------------- + +.. method:: XPathSelectorList.x(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 ``
`` tags and print their class attribute:: + + for node in x.x("//p"): + ... print node.x("@href") + +4. Extract textual data from all ``
`` tags without entities, as a list of
+ unicode strings::
+
+ x.x("//p/text()").extract_unquoted()
+
+ # the following line is wrong. extract_unquoted() should only be used
+ # with textual XPathSelectors
+ x.x("//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 ``
']
>>> for index, link in enumerate(links):
- print 'Link number %d points to url %s and image %s' % (index, link.x('@href').extract(), link.x('img/@src').extract())
+ args = (index, link.x('@href').extract(), link.x('img/@src').extract())
+ print 'Link number %d points to url %s and image %s' % args
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']
@@ -117,16 +173,3 @@ different calls, for example:::
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']
-There are some things to keep in mind here:
-
-1. | x() calls always return an XPathSelectorList, which is basically a list of selectors, with the extra ability of applying XPath or Regexp to each of its items and
- returning a new list.
- | That's why you can concatenate x() calls, because they always return XPathSelectorLists, and you can always reapply that method over them.
-2. x() calls are relative to the node your standing on, so selector.x('body/div[@id="mydiv"]') equals selector.x('body').x('div[@id="mydiv"]').
-3. The extract() method *always* returns a list, even if it contains only one element. Don't forget that.
-
-| You may also have noticed that I've used another method up there; the re() method.
-| This one is very useful when the data extracted by XPath is not enough and you *have to* (remember to not abuse of regexp) make an extra parsing of the information you've got.
-| In this cases, you just apply the re() method over any XPathSelector/XPathSelectorList you have with a compiled regexp pattern as the only argument, or a string with the pattern to be compiled.
-| Remember that the re() method *always* returns an already extracted list, which means that you can't go back to a node from the result of a re() call.
-
diff --git a/scrapy/trunk/docs/topics/shell.rst b/scrapy/trunk/docs/topics/shell.rst
index 2005234ef..c9f2d6476 100644
--- a/scrapy/trunk/docs/topics/shell.rst
+++ b/scrapy/trunk/docs/topics/shell.rst
@@ -61,9 +61,9 @@ These commands can be typed without the leading percent sign if you have
Custom Shell Objects
--------------------
-The console automatically makes some useful Scrapy objects available for the
-downloaded page, like the Response object and the XPath selectors (for both
-HTML and XML content).
+The console automatically creates some useful Scrapy objects for the downloaded
+page, like the :class:`~scrapy.http.Response` object and the
+:class:`~scrapy.xpath.XPathSelector` objects (for both HTML and XML content).
Those objects are:
@@ -75,9 +75,11 @@ Those objects are:
* ``response`` - a Response object of the downloaded page
- * ``hxs`` - a HtmlXPathSelector object for the Response of the downloaded page
+ * ``hxs`` - a :class:`~scrapy.xpath.HtmlXPathSelector` object for the Response
+ of the downloaded page
- * ``xxs`` - a XmlXPathSelector object for the Response of the downloaded page
+ * ``xxs`` - a :class:`~scrapy.xpath.XmlXPathSelector` object for the Response
+ of the downloaded page
* ``get