- Scrapy教程
- Scrapy - 主页
- Scrapy 基本概念
- Scrapy - 概述
- Scrapy - 环境
- Scrapy - 命令行工具
- Scrapy - 蜘蛛
- Scrapy - 选择器
- Scrapy - 项目
- Scrapy - 物品加载器
- Scrapy - 外壳
- Scrapy - 项目管道
- Scrapy - 饲料出口
- Scrapy - 请求和响应
- Scrapy - 链接提取器
- Scrapy-设置
- Scrapy - 异常
- Scrapy 现场项目
- Scrapy - 创建一个项目
- Scrapy - 定义一个项目
- Scrapy - 第一个蜘蛛
- Scrapy - 爬行
- Scrapy - 提取项目
- Scrapy - 使用项目
- Scrapy - 以下链接
- Scrapy - 抓取数据
- Scrapy 有用的资源
- Scrapy - 快速指南
- Scrapy - 有用的资源
- Scrapy - 讨论
Scrapy - 提取项目
描述
为了从网页中提取数据,Scrapy 使用一种称为基于XPath和CSS表达式的选择器的技术。以下是 XPath 表达式的一些示例 -
/html/head/title - 这将选择 HTML 文档的 <head> 元素内的 <title> 元素。
/html/head/title/text() - 这将选择同一 <title> 元素中的文本。
//td - 这将从 <td> 中选择所有元素。
//div[@class = "slice"] - 这将从div中选择包含属性 class = "slice" 的所有元素
选择器有四种基本方法,如下表所示 -
| 先生编号 | 方法及说明 |
|---|---|
| 1 | 提炼() 它返回一个 unicode 字符串以及所选数据。 |
| 2 | 关于() 它返回一个 unicode 字符串列表,当将正则表达式作为参数给出时提取该列表。 |
| 3 | xpath() 它返回一个选择器列表,该列表表示由作为参数给出的 xpath 表达式选择的节点。 |
| 4 | CSS() 它返回一个选择器列表,它表示由作为参数给出的 CSS 表达式选择的节点。 |
在 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.body和response.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