scrapy 选择器——迹忆客-ag捕鱼王app官网
当我们抓取网页时,需要使用称为选择器的机制提取 html 源的特定部分,该机制通过使用 xpath
或 css
表达式来实现。 选择器建立在 lxml 库之上,它以 python 语言处理 xml 和 html。
使用以下代码片段来定义选择器的不同概念
迹忆客
hello world!!!
构建选择器
我们可以通过传递文本或 textresponse
对象来构造选择器类实例。 根据提供的输入类型,选择器选择以下规则
from scrapy.selector import selector
from scrapy.http import htmlresponse
使用上面的代码,我们可以从文本构造为
selector(text = body).xpath('//span/text()').extract()
它将结果显示为
[u'hello world!!!']
我们可以从响应构造为
response = htmlresponse(url = 'http://mysite.com', body = body)
selector(response = response).xpath('//span/text()').extract()
结果显示如下
[u'hello world!!!']
使用选择器
使用上面的简单代码片段,我们可以构建用于选择标题标签中定义的文本的 xpath
,如下所示
response.selector.xpath('//title/text()')
现在,我们可以使用 .extract()
方法提取文本数据,如下所示
response.xpath('//title/text()').extract()
结果显示如下
[u'my website']
我们可以显示所有元素的名称,如下所示
response.xpath('//div[@class = "links"]/a/text()').extract()
它将元素显示为
link 1
link 2
link 3
如果要提取第一个元素,则使用.extract_first()
方法,如下所示
response.xpath('//div[@class = "links"]/a/text()').extract_first()
元素显示如下所示
link 1
嵌套选择器
使用上面的代码,可以使用 .xpath()
方法嵌套选择器来显示页面链接和图片来源,如下所示
links = response.xpath('//a[contains(@href, "image")]')
for index, link in enumerate(links):
args = (index, link.xpath('@href').extract(), link.xpath('img/@src').extract())
print 'the link %d pointing to url %s and image %s' % args
结果显示如下
link 1 pointing to url [u'one.html'] and image [u'image1.jpg']
link 2 pointing to url [u'two.html'] and image [u'image2.jpg']
link 3 pointing to url [u'three.html'] and image [u'image3.jpg']
使用正则表达式的选择器
scrapy 允许使用正则表达式提取数据,它使用 .re()
方法。 从上面的 html 代码中,我们将提取如下所示的图像名称
response.xpath('//a[contains(@href, "image")]/text()').re(r'name:\s*(.*)')
上面一行将图像名称显示为
[u'link 1',
u'link 2',
u'link 3']
使用相对 xpath
当我们使用以 /
开头的 xpath 时,嵌套选择器和 xpath 与文档的绝对路径相关,而不是选择器的相对路径。
如果要提取
元素,那么首先获取所有div元素
mydiv = response.xpath('//div')
接下来,我们可以提取其中的所有“p”元素,方法是在 xpath 前加上一个点作为 .//p
的前缀,如下所示
for p in mydiv.xpath('.//p').extract()
使用 exslt 扩展
exslt 是一个发布 xslt(可扩展样式表语言转换)扩展的社区,可将 xml 文档转换为 xhtml 文档。 我们可以将 exslt 扩展与 xpath 表达式中已注册的命名空间一起使用,如下表所示
序号 | 前缀 | 用法 | 命名空间 |
---|---|---|---|
1 | re | 正则表达式 | |
2 | set | set 操作 |
我们可以查看上一节使用正则表达式提取数据的简单代码格式。
有一些 xpath 技巧,在将 xpath 与 scrapy 选择器一起使用时很有用。 有关更多信息,请单击此链接