Scrapy教程

Scrapy 提取

说明

为了从网页中提取数据,Scrapy 使用一种称为选择器的技术,该技术基于 XPath 和 CSS 表达式。以下是 XPath 表达式的一些示例-
/html/head/title-这将选择 HTML 文档的 元素内的 <title> 元素。 /html/head/title/text()-这将选择同一 <title> 元素中的文本。 //td-这将选择 中的所有元素。 //div[@class = "slice"]-这将选择所有元素div 中包含属性 class = "slice" 的评论
选择器有四种基本方法,如下表所示-
方法和说明
extract()
它返回一个unicode字符串和选定的数据。
re()
它返回一个unicode字符串列表,当正则表达式作为参数给出时提取。
xpath()
它返回一个选择器列表,它表示由作为参数给出的 xpath 表达式选择的节点。
td>
css()
它返回一个选择器列表,它表示由作为参数给出的 CSS 表达式选择的节点。
td>

在 Shell 中使用选择器

要使用内置的 Scrapy shell 演示选择器,您需要安装 IPython在你的系统中。这里重要的是,在运行 Scrapy 时,URL 应该包含在引号中;否则带有"&"字符的 URL 将不起作用。您可以在项目的顶级目录中使用以下命令启动 shell-
scrapy shell "http://www.dmoz.org/Computers/Programming/Languages/Python/Books/"
shell 如下所示-
[ ... Scrapy log here ... ]
2014-01-23 17:11:42-0400 [scrapy] DEBUG: Crawled (200) 
<GET http://www.dmoz.org/Computers/Programming/Languages/Python/Books/>(referer: None)
[s] Available Scrapy objects:
[s]   crawler    <scrapy.crawler.Crawler object at 0x3636b50>
[s]   item       {}
[s]   request    <GET http://www.dmoz.org/Computers/Programming/Languages/Python/Books/>
[s]   response   <200 http://www.dmoz.org/Computers/Programming/Languages/Python/Books/>
[s]   settings   <scrapy.settings.Settings object at 0x3fadc50>
[s]   spider     <Spider 'default' at 0x3cebf50>
[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]:
当shell加载时,你可以分别使用 response.bodyresponse.header来访问body或header。同样,您可以使用 response.selector.xpath()response.selector.css() 对响应运行查询。
例如-
In [1]: response.xpath('//title')
Out[1]: [<Selector xpath = '//title' data = u'<title>My Book-Scrapy'>]
In [2]: response.xpath('//title').extract()
Out[2]: [u'<title>My Book-Scrapy: Index: Chapters</title>']
In [3]: response.xpath('//title/text()')
Out[3]: [<Selector xpath = '//title/text()' data = u'My Book-Scrapy: Index:'>]
In [4]: response.xpath('//title/text()').extract()
Out[4]: [u'My Book-Scrapy: Index: Chapters']
In [5]: response.xpath('//title/text()').re('(\w+):')
Out[5]: [u'Scrapy', u'Index', u'Chapters']

提取数据

要从普通 HTML 站点提取数据,我们必须检查站点的源代码以获取 XPath。检查后,您可以看到数据将在 ul 标签中。选择 li 标签内的元素。
以下代码行显示了不同类型数据的提取-
用于选择 li 标签内的数据-
response.xpath('//ul/li')
用于选择描述-
response.xpath('//ul/li/text()').extract()
用于选择网站标题-
response.xpath('//ul/li/a/text()').extract()
用于选择站点链接-
response.xpath('//ul/li/a/@href').extract()
以下代码演示了上述提取器的使用-
import scrapy
class MyprojectSpider(scrapy.Spider):
   name = "project"
   allowed_domains = ["dmoz.org"]
   
   start_urls = [
      "http://www.dmoz.org/Computers/Programming/Languages/Python/Books/",
      "http://www.dmoz.org/Computers/Programming/Languages/Python/Resources/"
   ]
   def parse(self, response):
      for sel in response.xpath('//ul/li'):
         title = sel.xpath('a/text()').extract()
         link = sel.xpath('a/@href').extract()
         desc = sel.xpath('text()').extract()
         print title, link, desc
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4