笨鸟编程-零基础入门Pyhton教程

 找回密码
 立即注册

Scrapy 教程

发布者: 笨鸟自学网



提取数据

学习如何使用scrappy提取数据的最佳方法是使用 Scrapy shell . 运行:

scrapy shell 'http://quotes.toscrape.com/page/1/'

注解

否则,在运行Scrapy命令时,请记住要在命令行中包含url。 & 字符)不起作用。

在Windows上,使用双引号:

scrapy shell "http://quotes.toscrape.com/page/1/"

您将看到类似的内容:

[ ... Scrapy log here ... ]
2016-09-19 12:09:27 [scrapy.core.engine] DEBUG: Crawled (200) <GET http://quotes.toscrape.com/page/1/> (referer: None)
[s] Available Scrapy objects:
[s]   scrapy     scrapy module (contains scrapy.Request, scrapy.Selector, etc)
[s]   crawler    <scrapy.crawler.Crawler object at 0x7fa91d888c90>
[s]   item       {}
[s]   request    <GET http://quotes.toscrape.com/page/1/>
[s]   response   <200 http://quotes.toscrape.com/page/1/>
[s]   settings   <scrapy.settings.Settings object at 0x7fa91d888c10>
[s]   spider     <DefaultSpider 'default' at 0x7fa91c8af990>
[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

使用shell,可以尝试使用 CSS 对于响应对象:

>>> response.css('title')
[<Selector xpath='descendant-or-self::title' data='<title>Quotes to Scrape</title>'>]

跑步的结果 response.css('title') 是一个名为 SelectorList ,它表示 Selector 对象,这些对象环绕XML/HTML元素,并允许您运行进一步的查询以细化选择或提取数据。

要从上述标题中提取文本,可以执行以下操作:

>>> response.css('title::text').getall()
['Quotes to Scrape']

这里有两件事需要注意:一是我们已经添加了 ::text 对于CSS查询,意味着我们只想直接选择内部的文本元素 <title> 元素。如果我们不指定 ::text ,我们将获得完整的title元素,包括其标记:

>>> response.css('title').getall()
['<title>Quotes to Scrape</title>']

另一件事是呼叫的结果 .getall() 是一个列表:选择器可能返回多个结果,因此我们提取所有结果。当您知道您只想要第一个结果时,如本例所示,您可以:

>>> response.css('title::text').get()
'Quotes to Scrape'

作为替代,你可以写下:

>>> response.css('title::text')[0].get()
'Quotes to Scrape'

访问 SelectorList 实例将引发 IndexError 如果没有结果,则异常::

>>> response.css('noelement')[0].get()
Traceback (most recent call last):
...
IndexError: list index out of range

您可能想要使用 .get() 直接在 SelectorList 实例,它返回 None 如果没有结果::

>>> response.css("noelement").get()

这里有一个教训:对于大多数抓取代码,您希望它能够对由于在页面上找不到的东西而导致的错误具有弹性,这样即使某些部分无法抓取,您至少可以 some 数据。

除此之外 getall() 和 get() 方法,也可以使用 re() 提取方法 regular expressions :

>>> response.css('title::text').re(r'Quotes.*')
['Quotes to Scrape']
>>> response.css('title::text').re(r'Q\w+')
['Quotes']
>>> response.css('title::text').re(r'(\w+) to (\w+)')
['Quotes', 'Scrape']

为了找到合适的CSS选择器,您可能会发现在Web浏览器的shell中使用 view(response) . 您可以使用浏览器的开发人员工具检查HTML并找到一个选择器(请参见 使用浏览器的开发人员工具进行抓取 

Selector Gadget 也是一个很好的工具,可以快速找到视觉上选中的元素的CSS选择器,它可以在许多浏览器中使用。


上一篇:安装指南下一篇:命令行工具

Archiver|手机版|笨鸟自学网 ( 粤ICP备20019910号 )

GMT+8, 2024-9-8 11:51 , Processed in 0.034292 second(s), 17 queries .

© 2001-2020

返回顶部