Scrapy shell没有hxs或xxs作为可用对象

时间:2014-08-10 23:47:42

标签: python scrapy ipython

首先,我遵循以下教程:http://doc.scrapy.org/en/0.16/intro/tutorial.html

我跑:

scrapy shell http://www.dmoz.org/Computers/Programming/Languages/Python/Books/

从教程中它说:“shell还实例化了两个选择器,一个用于HTML(在hxs变量中),另一个用于XML(在xxs变量中),具有此响应。所以让我们试试它们:”

我的输出没有hxs或xxs作为可用对象:

[s] Available Scrapy objects:
[s]   crawler    <scrapy.crawler.Crawler object at 0x03B33750>
[s]   item       {}
[s]   request    <GET http://tvtorrents.com/loggedin/show.do?id=3845>
[s]   response   <200 http://tvtorrents.com/login.do>
[s]   settings   <scrapy.settings.Settings object at 0x02718790>
[s]   spider     <Spider 'default' at 0x3e30190>
[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

我期待:

[s]   hxs        <HtmlXPathSelector (http://www.dmoz.org/Computers/Programming/Languages/Python/Books/) xpath=None>
[s]   xxs        <XmlXPathSelector (http://www.dmoz.org/Computers/Programming/Languages/Python/Books/) xpath=None>

作为“可用对象”。

这可以防止我像这样改进输出:

In [1]: hxs.select('//title')
Out[1]: [<HtmlXPathSelector (title) xpath=//title>]

感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

这是一个版本问题:您使用的教程是为Scrapy 0.16编写的,您使用的是较新版本的Scrapy。使用此链接获取最新的可用教程:http://doc.scrapy.org/en/latest/intro/tutorial.html

而且,仅供参考,您现在应该使用response.xpath()

$ scrapy shell http://stackoverflow.com
>>> response.xpath('//title')
[<Selector xpath='//title' data=u'<title>Stack Overflow</title>'>]
>>> response.xpath('//title').extract()
[u'<title>Stack Overflow</title>']
>>> response.xpath('//title/text()').extract()
[u'Stack Overflow']