Scrapy, a fast high-level web crawling & scraping framework for Python.
DOC document response.follow methods; expand the tutorial
Mikhail Korobov5b79c6a679b6
diff --git a/docs/intro/tutorial.rst b/docs/intro/tutorial.rstindex d47bf69e5..3b3bd8d21 100644--- a/docs/intro/tutorial.rst+++ b/docs/intro/tutorial.rst@@ -399,7 +399,7 @@ quotes elements and put them together into a Python dictionary::>>>Extracting data in our spider-------------------------------+-----------------------------Let's get back to our spider. Until now, it doesn't extract any data inparticular, just saves the whole HTML page to a local file. Let's integrate the@@ -551,8 +551,14 @@ In our example, it creates a sort of loop, following all the links to the next puntil it doesn't find one -- handy for crawling blogs, forums and other sites withpagination.++.. _response-follow-example:++A shortcut for creating Requests+--------------------------------+As a shortcut for creating Request objects you can use-:meth:`response.follow <scrapy.http.TextResponse.follow>` method::+:meth:`response.follow <scrapy.http.TextResponse.follow>`::import scrapy@@ -571,13 +577,32 @@ As a shortcut for creating Request objects you can use'tags': quote.css('div.tags a.tag::text').extract(),}- for href in response.css('li.next a::attr(href)'):- yield response.follow(href, callback=self.parse)+ next_page = response.css('li.next a::attr(href)').extract_first()+ if next_page is not None:+ yield response.follow(next_page, callback=self.parse)++Unlike scrapy.Request, ``response.follow`` supports relative URLs directly - no+need to call urljoin. Note that ``response.follow`` just returns a Request+instance; you still have to yield this Request.++You can also pass a selector to ``response.follow`` instead of a string;+this selector should extract necessary attributes::++ for href in response.css('li.next a::attr(href)'):+ yield response.follow(href, callback=self.parse)++For ``<a>`` elements there is a shortcut: ``response.follow`` uses their href+attribute automatically. So the code can be shortened further::++ for a in response.css('li.next a'):+ yield response.follow(a, callback=self.parse)++.. note::-Unlike scrapy.Request, ``response.follow`` supports-relative URLs directly; you can also pass a selector to it instead of-a string. Note that ``response.follow`` just returns a Request instance;-you still have to yield this Request.+ ``response.follow(response.css('li.next a'))`` is not valid because+ ``response.css`` returns a list-like object with selectors for all results,+ not a single selector. A ``for`` loop like in the example above, or+ ``response.follow(response.css('li.next a')[0])`` is fine.More examples and patterns--------------------------diff --git a/docs/topics/request-response.rst b/docs/topics/request-response.rstindex 71050fddd..3e80f18b5 100644--- a/docs/topics/request-response.rst+++ b/docs/topics/request-response.rst@@ -597,6 +597,9 @@ Response objectsurlparse.urljoin(response.url, url)+ .. automethod:: Response.follow++.. _urlparse.urljoin: https://docs.python.org/2/library/urlparse.html#urlparse.urljoin.. _topics-request-response-ref-response-subclasses:@@ -683,9 +686,7 @@ TextResponse objectsresponse.css('p')- .. method:: TextResponse.follow(url, ...)-- Return a scrapy.Request instance to follow a link ``url``.+ .. automethod:: TextResponse.follow.. method:: TextResponse.body_as_unicode()diff --git a/scrapy/http/response/__init__.py b/scrapy/http/response/__init__.pyindex e5fb4eef8..434d87eab 100644--- a/scrapy/http/response/__init__.py+++ b/scrapy/http/response/__init__.py@@ -109,13 +109,14 @@ class Response(object_ref):dont_filter=False, errback=None):# type: (...) -> Request"""- Return a scrapy.Request instance to follow a link ``url``.-- ``url`` can be:-- * absolute URL;- * relative URL;- * scrapy.link.Link object.+ Return a :class:`~.Request` instance to follow a link ``url``.+ It accepts the same arguments as ``Request.__init__`` method,+ but ``url`` can be a relative URL or a ``scrapy.link.Link`` object,+ not only an absolute URL.++ :class:`~.TextResponse` provides a :meth:`~.TextResponse.follow`+ method which supports selectors in addition to absolute/relative URLs+ and Link objects."""if isinstance(url, Link):url = url.urldiff --git a/scrapy/http/response/text.py b/scrapy/http/response/text.pyindex 3c360bcf9..6415e191a 100644--- a/scrapy/http/response/text.py+++ b/scrapy/http/response/text.py@@ -13,7 +13,6 @@ from w3lib.encoding import html_to_unicode, resolve_encoding, \html_body_declared_encoding, http_content_type_encodingfrom w3lib.html import strip_html5_whitespace-from scrapy.link import Linkfrom scrapy.http.request import Requestfrom scrapy.http.response import Responsefrom scrapy.utils.response import get_base_url@@ -127,18 +126,19 @@ class TextResponse(Response):dont_filter=False, errback=None):# type: (...) -> Request"""- Return a scrapy.Request instance to follow a link ``url``.-- ``url`` can be:-- * absolute URL;- * relative URL;- * scrapy.link.Link object (e.g. a link extractor result);- * attribute Selector (not SelectorList) - e.g.+ Return a :class:`~.Request` instance to follow a link ``url``.+ It accepts the same arguments as ``Request.__init__`` method,+ but ``url`` can be not only an absolute URL, but also++ * a relative URL;+ * a scrapy.link.Link object (e.g. a link extractor result);+ * an attribute Selector (not SelectorList) - e.g.``response.css('a::attr(href)')[0]`` or``response.xpath('//img/@src')[0]``.* a Selector for ``<a>`` element, e.g.``response.css('a.my_link')[0]``.++ See :ref:`response-follow-example` for usage examples."""if isinstance(url, parsel.Selector):url = _url_from_selector(url)