`` element. Equivalent CSS selector: ``html > head > title ::text``.
-* ``//td``: selects all the ``| `` elements
+* ``//td``: selects all the `` | `` elements from the whole document.
+ Equivalent CSS selector: ``td``.
* ``//div[@class="mine"]``: selects all ``div`` elements which contain an
- attribute ``class="mine"``
+ attribute ``class="mine"``. Equivalent CSS selector: ``div.mine``.
These are just a couple of simple examples of what you can do with XPath, but
XPath expressions are indeed much more powerful. To learn more about XPath, we
@@ -220,7 +220,7 @@ to think in XPath" `_.
Because of this, we encourage you to learn about XPath even if you
already know how to construct CSS selectors.
-For working with CSS and XPath expressions, Scrapy provides
+For working with CSS and XPath expressions, Scrapy provides the
:class:`~scrapy.selector.Selector` class and convenient shortcuts to avoid
instantiating selectors yourself every time you need to select something from a
response.
@@ -255,7 +255,7 @@ installed on your system.
To start a shell, you must go to the project's top level directory and run::
- scrapy shell "http://www.dmoz.org/Computers/Programming/Languages/Python/Books/"
+ scrapy shell "http://quotes.toscrape.com"
.. note::
@@ -267,20 +267,20 @@ This is what the shell looks like::
[ ... Scrapy log here ... ]
- 2014-01-23 17:11:42-0400 [scrapy] DEBUG: Crawled (200) (referer: None)
+ 2016-09-01 18:14:39 [scrapy] DEBUG: Crawled (200) (referer: None)
[s] Available Scrapy objects:
- [s] crawler
+ [s] crawler
[s] item {}
- [s] request
- [s] response <200 http://www.dmoz.org/Computers/Programming/Languages/Python/Books/>
- [s] settings
- [s] spider
+ [s] request
+ [s] response <200 http://quotes.toscrape.com>
+ [s] settings
+ [s] spider
[s] Useful shortcuts:
[s] shelp() Shell help (print this help)
[s] fetch(req_or_url) Fetch request (or URL) and update local objects
[s] view(response) View response in a browser
-
- In [1]:
+
+ >>>
After the shell loads, you will have the response fetched in a local
``response`` variable, so if you type ``response.body`` you will see the body
@@ -297,19 +297,19 @@ or ``response.css()`` which map directly to ``response.selector.xpath()`` and
So let's try it::
In [1]: response.xpath('//title')
- Out[1]: [Open Directory - Computers: Progr'>]
-
+ Out[1]: [Quotes to Scrape'>]
+
In [2]: response.xpath('//title').extract()
- Out[2]: [u'Open Directory - Computers: Programming: Languages: Python: Books']
-
+ Out[2]: [u'Quotes to Scrape']
+
In [3]: response.xpath('//title/text()')
- Out[3]: []
-
+ Out[3]: []
+
In [4]: response.xpath('//title/text()').extract()
- Out[4]: [u'Open Directory - Computers: Programming: Languages: Python: Books']
-
- In [5]: response.xpath('//title/text()').re('(\w+):')
- Out[5]: [u'Computers', u'Programming', u'Languages', u'Python']
+ Out[4]: [u'Quotes to Scrape']
+
+ In [11]: response.xpath('//title/text()').re('(\w+)')
+ Out[11]: [u'Quotes', u'to', u'Scrape']
Extracting the data
^^^^^^^^^^^^^^^^^^^
@@ -322,35 +322,42 @@ there could become a very tedious task. To make it easier, you can
use Firefox Developer Tools or some Firefox extensions like Firebug. For more
information see :ref:`topics-firebug` and :ref:`topics-firefox`.
-After inspecting the page source, you'll find that the web site's information
-is inside a ```` element, in fact the *second* `` |