选择器
当您抓取网页时,最常见的任务是从 HTML 源代码中提取数据。有几个库可以实现此目的,例如
BeautifulSoup 是 Python 程序员中非常流行的网页抓取库,它根据 HTML 代码的结构构建 Python 对象,并且能够很好地处理不规范的标记,但它有一个缺点:速度慢。
lxml 是一个 XML 解析库(也解析 HTML),其 Pythonic API 基于
ElementTree。(lxml 不是 Python 标准库的一部分。)
Scrapy 带有自己的数据提取机制。它们被称为选择器,因为它们“选择”HTML 文档中由 XPath 或 CSS 表达式指定的特定部分。
XPath 是一种用于选择 XML 文档中节点的语言,它也可以用于 HTML。CSS 是一种用于为 HTML 文档应用样式的语言。它定义了选择器以将这些样式与特定的 HTML 元素关联起来。
注意
Scrapy Selectors 是 parsel 库的一个薄封装;此封装的目的是为了更好地与 Scrapy Response 对象集成。
parsel 是一个独立的网页抓取库,可以在没有 Scrapy 的情况下使用。它底层使用 lxml 库,并在 lxml API 之上实现了一个简单的 API。这意味着 Scrapy 选择器在速度和解析精度上与 lxml 非常相似。
使用选择器
构建选择器
Response 对象在 .selector 属性上暴露一个 Selector 实例
>>> response.selector.xpath("//span/text()").get()
'good'
使用 XPath 和 CSS 查询响应非常常见,因此响应包含另外两个快捷方式:response.xpath() 和 response.css()
>>> response.xpath("//span/text()").get()
'good'
>>> response.css("span::text").get()
'good'
Scrapy 选择器是 Selector 类的实例,通过传递 TextResponse 对象或将标记作为字符串(在 text 参数中)来构建。
通常不需要手动构建 Scrapy 选择器:response 对象在 Spider 回调中可用,因此在大多数情况下,使用 response.css() 和 response.xpath() 快捷方式更方便。通过使用 response.selector 或这些快捷方式之一,您还可以确保响应体只被解析一次。
但如果需要,可以直接使用 Selector。从文本构建
>>> from scrapy.selector import Selector
>>> body = "<html><body><span>good</span></body></html>"
>>> Selector(text=body).xpath("//span/text()").get()
'good'
从响应构建 - HtmlResponse 是 TextResponse 的子类之一
>>> from scrapy.selector import Selector
>>> from scrapy.http import HtmlResponse
>>> response = HtmlResponse(url="http://example.com", body=body, encoding="utf-8")
>>> Selector(response=response).xpath("//span/text()").get()
'good'
Selector 根据输入类型自动选择最佳解析规则(XML 或 HTML)。
使用选择器
为了解释如何使用选择器,我们将使用 Scrapy shell(提供交互式测试)以及 Scrapy 文档服务器中的一个示例页面
为了完整性,这里是其完整的 HTML 代码
<!DOCTYPE html>
<html>
<head>
<base href='http://example.com/' />
<title>Example website</title>
</head>
<body>
<div id='images'>
<a href='image1.html'>Name: My image 1 <br /><img src='image1_thumb.jpg' alt='image1'/></a>
<a href='image2.html'>Name: My image 2 <br /><img src='image2_thumb.jpg' alt='image2'/></a>
<a href='image3.html'>Name: My image 3 <br /><img src='image3_thumb.jpg' alt='image3'/></a>
<a href='image4.html'>Name: My image 4 <br /><img src='image4_thumb.jpg' alt='image4'/></a>
<a href='image5.html'>Name: My image 5 <br /><img src='image5_thumb.jpg' alt='image5'/></a>
</div>
</body>
</html>
首先,让我们打开 shell
scrapy shell https://docs.scrapy.net.cn/en/latest/_static/selectors-sample1.html
然后,在 shell 加载后,您将拥有可作为 response shell 变量的响应,以及其附加的选择器在 response.selector 属性中。
由于我们处理的是 HTML,选择器将自动使用 HTML 解析器。
因此,通过查看该页面的 HTML 代码,让我们构建一个 XPath 来选择标题标签内的文本
>>> response.xpath("//title/text()")
[<Selector query='//title/text()' data='Example website'>]
要实际提取文本数据,您必须调用选择器的 .get() 或 .getall() 方法,如下所示
>>> response.xpath("//title/text()").getall()
['Example website']
>>> response.xpath("//title/text()").get()
'Example website'
.get() 始终返回单个结果;如果有多个匹配项,则返回第一个匹配项的内容;如果没有匹配项,则返回 None。.getall() 返回包含所有结果的列表。
请注意,CSS 选择器可以使用 CSS3 伪元素选择文本或属性节点
>>> response.css("title::text").get()
'Example website'
如您所见,.xpath() 和 .css() 方法返回一个 SelectorList 实例,它是一个新选择器的列表。此 API 可用于快速选择嵌套数据
>>> response.css("img").xpath("@src").getall()
['image1_thumb.jpg',
'image2_thumb.jpg',
'image3_thumb.jpg',
'image4_thumb.jpg',
'image5_thumb.jpg']
如果您只想提取第一个匹配的元素,可以调用选择器的 .get() 方法(或其在早期 Scrapy 版本中常用的别名 .extract_first())
>>> response.xpath('//div[@id="images"]/a/text()').get()
'Name: My image 1 '
如果没有找到元素,它将返回 None
>>> response.xpath('//div[@id="not-exists"]/text()').get() is None
True
可以提供一个默认返回值作为参数,以替代 None
>>> response.xpath('//div[@id="not-exists"]/text()').get(default="not-found")
'not-found'
除了使用例如 '@src' XPath,还可以使用 Selector 的 .attrib 属性查询属性
>>> [img.attrib["src"] for img in response.css("img")]
['image1_thumb.jpg',
'image2_thumb.jpg',
'image3_thumb.jpg',
'image4_thumb.jpg',
'image5_thumb.jpg']
作为快捷方式,.attrib 也直接在 SelectorList 上可用;它返回第一个匹配元素的属性
>>> response.css("img").attrib["src"]
'image1_thumb.jpg'
这在只期望单个结果时最有用,例如通过 ID 选择或选择网页上的唯一元素时
>>> response.css("base").attrib["href"]
'http://example.com/'
现在我们将获取基本 URL 和一些图片链接
>>> response.xpath("//base/@href").get()
'http://example.com/'
>>> response.css("base::attr(href)").get()
'http://example.com/'
>>> response.css("base").attrib["href"]
'http://example.com/'
>>> response.xpath('//a[contains(@href, "image")]/@href').getall()
['image1.html',
'image2.html',
'image3.html',
'image4.html',
'image5.html']
>>> response.css("a[href*=image]::attr(href)").getall()
['image1.html',
'image2.html',
'image3.html',
'image4.html',
'image5.html']
>>> response.xpath('//a[contains(@href, "image")]/img/@src').getall()
['image1_thumb.jpg',
'image2_thumb.jpg',
'image3_thumb.jpg',
'image4_thumb.jpg',
'image5_thumb.jpg']
>>> response.css("a[href*=image] img::attr(src)").getall()
['image1_thumb.jpg',
'image2_thumb.jpg',
'image3_thumb.jpg',
'image4_thumb.jpg',
'image5_thumb.jpg']
CSS 选择器扩展
根据 W3C 标准,CSS 选择器不支持选择文本节点或属性值。但在网页抓取环境中,选择这些是如此重要,以至于 Scrapy (parsel) 实现了一些 非标准伪元素
要选择文本节点,请使用
::text要选择属性值,请使用
::attr(name),其中 name 是您想要其值的属性名称
示例
title::text选择后代<title>元素的子文本节点
>>> response.css("title::text").get()
'Example website'
*::text选择当前选择器上下文的所有后代文本节点
..skip: next .. code-block:: pycon
>>> response.css("#images *::text").getall()
['\n ',
'Name: My image 1 ',
'\n ',
'Name: My image 2 ',
'\n ',
'Name: My image 3 ',
'\n ',
'Name: My image 4 ',
'\n ',
'Name: My image 5 ',
'\n ']
如果
foo元素存在但没有包含文本(即文本为空),foo::text不会返回任何结果
>>> response.css("img::text").getall()
[]
This means ``.css('foo::text').get()`` could return None even if an element
exists. Use ``default=''`` if you always want a string:
>>> response.css("img::text").get()
>>> response.css("img::text").get(default="")
''
a::attr(href)选择后代链接的 href 属性值
>>> response.css("a::attr(href)").getall()
['image1.html',
'image2.html',
'image3.html',
'image4.html',
'image5.html']
注意
另请参阅:选择元素属性。
注意
您不能链式使用这些伪元素。但实际上这样做也没有多大意义:文本节点没有属性,而属性值本身就是字符串值,没有子节点。
嵌套选择器
选择方法(.xpath() 或 .css())返回一个相同类型的选择器列表,因此您也可以为这些选择器调用选择方法。这是一个示例
>>> links = response.xpath('//a[contains(@href, "image")]')
>>> links.getall()
['<a href="image1.html">Name: My image 1 <br><img src="image1_thumb.jpg" alt="image1"></a>',
'<a href="image2.html">Name: My image 2 <br><img src="image2_thumb.jpg" alt="image2"></a>',
'<a href="image3.html">Name: My image 3 <br><img src="image3_thumb.jpg" alt="image3"></a>',
'<a href="image4.html">Name: My image 4 <br><img src="image4_thumb.jpg" alt="image4"></a>',
'<a href="image5.html">Name: My image 5 <br><img src="image5_thumb.jpg" alt="image5"></a>']
>>> for index, link in enumerate(links):
... href_xpath = link.xpath("@href").get()
... img_xpath = link.xpath("img/@src").get()
... print(f"Link number {index} points to url {href_xpath!r} and image {img_xpath!r}")
...
Link number 0 points to url 'image1.html' and image 'image1_thumb.jpg'
Link number 1 points to url 'image2.html' and image 'image2_thumb.jpg'
Link number 2 points to url 'image3.html' and image 'image3_thumb.jpg'
Link number 3 points to url 'image4.html' and image 'image4_thumb.jpg'
Link number 4 points to url 'image5.html' and image 'image5_thumb.jpg'
选择元素属性
有几种方法可以获取属性值。首先,可以使用 XPath 语法
>>> response.xpath("//a/@href").getall()
['image1.html', 'image2.html', 'image3.html', 'image4.html', 'image5.html']
XPath 语法有一些优点:它是一个标准的 XPath 功能,并且 @attributes 可以用于 XPath 表达式的其他部分 - 例如,可以按属性值进行过滤。
Scrapy 还提供了一个 CSS 选择器扩展(::attr(...)),允许获取属性值
>>> response.css("a::attr(href)").getall()
['image1.html', 'image2.html', 'image3.html', 'image4.html', 'image5.html']
除此之外,Selector 还有一个 .attrib 属性。如果您喜欢在 Python 代码中查找属性,而不使用 XPath 或 CSS 扩展,则可以使用它
>>> [a.attrib["href"] for a in response.css("a")]
['image1.html', 'image2.html', 'image3.html', 'image4.html', 'image5.html']
此属性也适用于 SelectorList;它返回第一个匹配元素的属性字典。当预期选择器只返回单个结果时(例如,通过元素 ID 选择,或选择页面上的唯一元素时),使用它很方便
>>> response.css("base").attrib
{'href': 'http://example.com/'}
>>> response.css("base").attrib["href"]
'http://example.com/'
空 SelectorList 的 .attrib 属性是空的
>>> response.css("foo").attrib
{}
将选择器与正则表达式结合使用
Selector 还有一个 .re() 方法用于使用正则表达式提取数据。然而,与使用 .xpath() 或 .css() 方法不同,.re() 返回一个字符串列表。因此,您不能构造嵌套的 .re() 调用。
这是一个从上述 HTML 代码中提取图片名称的示例
>>> response.xpath('//a[contains(@href, "image")]/text()').re(r"Name:\s*(.*)")
['My image 1 ',
'My image 2 ',
'My image 3 ',
'My image 4 ',
'My image 5 ']
对于 .re(),还有一个辅助函数与 .get()(及其别名 .extract_first())相对应,名为 .re_first()。使用它来提取第一个匹配的字符串
>>> response.xpath('//a[contains(@href, "image")]/text()').re_first(r"Name:\s*(.*)")
'My image 1 '
extract() 和 extract_first()
如果您是 Scrapy 的长期用户,您可能熟悉 .extract() 和 .extract_first() 选择器方法。许多博客文章和教程也使用它们。Scrapy 仍然支持这些方法,没有计划弃用它们。
然而,Scrapy 的使用文档现在使用 .get() 和 .getall() 方法编写。我们认为这些新方法会产生更简洁、更可读的代码。
以下示例展示了这些方法如何相互映射。
SelectorList.get()与SelectorList.extract_first()相同
>>> response.css("a::attr(href)").get()
'image1.html'
>>> response.css("a::attr(href)").extract_first()
'image1.html'
SelectorList.getall()与SelectorList.extract()相同
>>> response.css("a::attr(href)").getall()
['image1.html', 'image2.html', 'image3.html', 'image4.html', 'image5.html']
>>> response.css("a::attr(href)").extract()
['image1.html', 'image2.html', 'image3.html', 'image4.html', 'image5.html']
Selector.get()与Selector.extract()相同
>>> response.css("a::attr(href)")[0].get()
'image1.html'
>>> response.css("a::attr(href)")[0].extract()
'image1.html'
为了保持一致性,还有
Selector.getall(),它返回一个列表
>>> response.css("a::attr(href)")[0].getall()
['image1.html']
因此,主要区别在于 .get() 和 .getall() 方法的输出更具可预测性:.get() 总是返回单个结果,.getall() 总是返回所有提取结果的列表。使用 .extract() 方法时,结果是否为列表并不总是显而易见的;要获得单个结果,应调用 .extract() 或 .extract_first()。
使用 XPath
这里有一些提示,可以帮助您有效地将 XPath 与 Scrapy 选择器结合使用。如果您对 XPath 还不是很熟悉,您可能想先查看这个 XPath 教程。
注意
其中一些提示基于 Zyte 博客的这篇文章。
使用相对 XPath
请记住,如果您正在嵌套选择器并使用以 / 开头的 XPath,则该 XPath 将相对于文档是绝对的,而不是相对于您调用它的 Selector。
例如,假设您想提取 <div> 元素内的所有 <p> 元素。首先,您将获取所有 <div> 元素
>>> divs = response.xpath("//div")
起初,您可能会尝试使用以下方法,这是错误的,因为它实际上从文档中提取了所有 <p> 元素,而不仅仅是 <div> 元素内的元素
>>> for p in divs.xpath("//p"): # this is wrong - gets all <p> from the whole document
... print(p.get())
...
这是正确的做法(请注意 .//p XPath 前的圆点)
>>> for p in divs.xpath(".//p"): # extracts all <p> inside
... print(p.get())
...
另一个常见情况是提取所有直接的 <p> 子元素
>>> for p in divs.xpath("p"):
... print(p.get())
...
有关相对 XPath 的更多详细信息,请参阅 XPath 规范中的 位置路径 部分。
按类查询时,考虑使用 CSS
因为一个元素可以包含多个 CSS 类,所以通过类选择元素的 XPath 方式相当冗长
*[contains(concat(' ', normalize-space(@class), ' '), ' someclass ')]
如果您使用 @class='someclass',您可能会错过具有其他类的元素;如果您只使用 contains(@class, 'someclass') 来弥补,如果它们具有共享字符串 someclass 的不同类名,您最终可能会得到比您想要的更多的元素。
事实证明,Scrapy 选择器允许您链式使用选择器,因此大多数时候您可以直接使用 CSS 按类选择,然后在需要时切换到 XPath
>>> from scrapy import Selector
>>> sel = Selector(
... text='<div class="hero shout"><time datetime="2014-07-23 19:00">Special date</time></div>'
... )
>>> sel.css(".shout").xpath("./time/@datetime").getall()
['2014-07-23 19:00']
这比上面显示的冗长 XPath 技巧更简洁。只需记住在后续的 XPath 表达式中使用 .。
注意 //node[1] 和 (//node)[1] 之间的区别
//node[1] 选择所有在其各自父级下首次出现的节点。
(//node)[1] 选择文档中的所有节点,然后只获取其中的第一个。
示例:
>>> from scrapy import Selector
>>> sel = Selector(
... text="""
... <ul class="list">
... <li>1</li>
... <li>2</li>
... <li>3</li>
... </ul>
... <ul class="list">
... <li>4</li>
... <li>5</li>
... <li>6</li>
... </ul>"""
... )
>>> xp = lambda x: sel.xpath(x).getall()
这会获取在其父级下的所有第一个 <li> 元素
>>> xp("//li[1]")
['<li>1</li>', '<li>4</li>']
这会获取整个文档中的第一个 <li> 元素
>>> xp("(//li)[1]")
['<li>1</li>']
这会获取 <ul> 父级下的所有第一个 <li> 元素
>>> xp("//ul/li[1]")
['<li>1</li>', '<li>4</li>']
这会获取整个文档中 <ul> 父级下的第一个 <li> 元素
>>> xp("(//ul/li)[1]")
['<li>1</li>']
在条件中使用文本节点
当您需要将文本内容作为参数传递给 XPath 字符串函数时,请避免使用 .//text(),而应直接使用 .。
这是因为表达式 .//text() 会产生一个文本元素的集合——一个 节点集。当节点集转换为字符串时(当它作为参数传递给诸如 contains() 或 starts-with() 之类的字符串函数时会发生这种情况),它只返回第一个元素的文本。
示例:
>>> from scrapy import Selector
>>> sel = Selector(
... text='<a href="#">Click here to go to the <strong>Next Page</strong></a>'
... )
将 节点集 转换为字符串
>>> sel.xpath("//a//text()").getall() # take a peek at the node-set
['Click here to go to the ', 'Next Page']
>>> sel.xpath("string(//a[1]//text())").getall() # convert it to string
['Click here to go to the ']
然而,将 节点 转换为字符串会将其自身的文本及其所有后代的文本组合在一起
>>> sel.xpath("//a[1]").getall() # select the first node
['<a href="#">Click here to go to the <strong>Next Page</strong></a>']
>>> sel.xpath("string(//a[1])").getall() # convert it to string
['Click here to go to the Next Page']
因此,在这种情况下,使用 .//text() 节点集将不会选择任何内容
>>> sel.xpath("//a[contains(.//text(), 'Next Page')]").getall()
[]
但使用 . 表示节点,则可以正常工作
>>> sel.xpath("//a[contains(., 'Next Page')]").getall()
['<a href="#">Click here to go to the <strong>Next Page</strong></a>']
XPath 表达式中的变量
XPath 允许您在 XPath 表达式中引用变量,使用 $somevariable 语法。这与 SQL 世界中的参数化查询或预处理语句有些相似,您将查询中的某些参数替换为 ? 等占位符,然后这些占位符将替换为随查询传递的值。
这是一个根据元素的“id”属性值进行匹配的示例,而无需硬编码(这在前面已经展示过)
>>> # `$val` used in the expression, a `val` argument needs to be passed
>>> response.xpath("//div[@id=$val]/a/text()", val="images").get()
'Name: My image 1 '
这是另一个示例,用于查找包含五个 <a> 子元素的 <div> 标签的“id”属性(这里我们将值 5 作为整数传递)
>>> response.xpath("//div[count(a)=$cnt]/@id", cnt=5).get()
'images'
所有变量引用在调用 .xpath() 时都必须有一个绑定值(否则您将收到 ValueError: XPath error: 异常)。这通过传递尽可能多的命名参数来完成。
移除命名空间
在处理抓取项目时,通常很方便地完全摆脱命名空间,只使用元素名称,以编写更简单/方便的 XPath。您可以使用 Selector.remove_namespaces() 方法来实现这一点。
让我们用 Python Insider 博客的 Atom feed 来展示一个示例。
首先,我们用想要抓取的 URL 打开 shell
$ scrapy shell https://feeds.feedburner.com/PythonInsider
文件开头如下
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet ...
<feed xmlns="https://w3org.cn/2005/Atom"
xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/"
xmlns:blogger="http://schemas.google.com/blogger/2008"
xmlns:georss="http://www.georss.org/georss"
xmlns:gd="http://schemas.google.com/g/2005"
xmlns:thr="http://purl.org/syndication/thread/1.0"
xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">
...
您可以看到几个命名空间声明,包括一个默认的 "https://w3org.cn/2005/Atom" 和另一个使用 gd: 前缀的 "http://schemas.google.com/g/2005"。
进入 shell 后,我们可以尝试选择所有 <link> 对象,并发现它不起作用(因为 Atom XML 命名空间混淆了这些节点)
>>> response.xpath("//link")
[]
但一旦我们调用 Selector.remove_namespaces() 方法,所有节点都可以直接通过它们的名称访问
>>> response.selector.remove_namespaces()
>>> response.xpath("//link")
[<Selector query='//link' data='<link rel="alternate" type="text/html" h'>,
<Selector query='//link' data='<link rel="next" type="application/atom+'>,
...
如果您想知道为什么命名空间移除过程不总是默认调用,而是需要手动调用,这有两个原因,按相关性顺序排列是
移除命名空间需要迭代并修改文档中的所有节点,这对于 Scrapy 抓取的所有文档来说,默认执行是一项相当昂贵的操作
在某些元素名称在命名空间之间冲突的情况下,可能确实需要使用命名空间。不过,这些情况非常罕见。
使用 EXSLT 扩展
Scrapy 选择器构建于 lxml 之上,因此支持一些 EXSLT 扩展,并预注册了这些命名空间,可在 XPath 表达式中使用
前缀 |
命名空间 |
用法 |
|---|---|---|
re |
http://exslt.org/regular-expressions |
|
set |
http://exslt.org/sets |
正则表达式
例如,当 XPath 的 starts-with() 或 contains() 不足以满足需求时,test() 函数会非常有用。
示例:选择列表中“class”属性以数字结尾的链接
>>> from scrapy import Selector
>>> doc = """
... <div>
... <ul>
... <li class="item-0"><a href="link1.html">first item</a></li>
... <li class="item-1"><a href="link2.html">second item</a></li>
... <li class="item-inactive"><a href="link3.html">third item</a></li>
... <li class="item-1"><a href="link4.html">fourth item</a></li>
... <li class="item-0"><a href="link5.html">fifth item</a></li>
... </ul>
... </div>
... """
>>> sel = Selector(text=doc, type="html")
>>> sel.xpath("//li//@href").getall()
['link1.html', 'link2.html', 'link3.html', 'link4.html', 'link5.html']
>>> sel.xpath(r'//li[re:test(@class, "item-\d$")]//@href').getall()
['link1.html', 'link2.html', 'link4.html', 'link5.html']
警告
C 库 libxslt 不原生支持 EXSLT 正则表达式,因此 lxml 的实现使用钩子连接到 Python 的 re 模块。因此,在 XPath 表达式中使用正则表达式函数可能会带来一些性能损失。
集合操作
例如,在提取文本元素之前,这些功能可用于排除文档树的某些部分,这会很方便。
示例:提取微数据(示例内容取自 https://schema.org/Product),包含多组 itemscope 和对应的 itemprop
>>> doc = """
... <div itemscope itemtype="http://schema.org/Product">
... <span itemprop="name">Kenmore White 17" Microwave</span>
... <img src="kenmore-microwave-17in.jpg" alt='Kenmore 17" Microwave' />
... <div itemprop="aggregateRating"
... itemscope itemtype="http://schema.org/AggregateRating">
... Rated <span itemprop="ratingValue">3.5</span>/5
... based on <span itemprop="reviewCount">11</span> customer reviews
... </div>
... <div itemprop="offers" itemscope itemtype="http://schema.org/Offer">
... <span itemprop="price">$55.00</span>
... <link itemprop="availability" href="http://schema.org/InStock" />In stock
... </div>
... Product description:
... <span itemprop="description">0.7 cubic feet countertop microwave.
... Has six preset cooking categories and convenience features like
... Add-A-Minute and Child Lock.</span>
... Customer reviews:
... <div itemprop="review" itemscope itemtype="http://schema.org/Review">
... <span itemprop="name">Not a happy camper</span> -
... by <span itemprop="author">Ellie</span>,
... <meta itemprop="datePublished" content="2011-04-01">April 1, 2011
... <div itemprop="reviewRating" itemscope itemtype="http://schema.org/Rating">
... <meta itemprop="worstRating" content = "1">
... <span itemprop="ratingValue">1</span>/
... <span itemprop="bestRating">5</span>stars
... </div>
... <span itemprop="description">The lamp burned out and now I have to replace
... it. </span>
... </div>
... <div itemprop="review" itemscope itemtype="http://schema.org/Review">
... <span itemprop="name">Value purchase</span> -
... by <span itemprop="author">Lucas</span>,
... <meta itemprop="datePublished" content="2011-03-25">March 25, 2011
... <div itemprop="reviewRating" itemscope itemtype="http://schema.org/Rating">
... <meta itemprop="worstRating" content = "1"/>
... <span itemprop="ratingValue">4</span>/
... <span itemprop="bestRating">5</span>stars
... </div>
... <span itemprop="description">Great microwave for the price. It is small and
... fits in my apartment.</span>
... </div>
... ...
... </div>
... """
>>> sel = Selector(text=doc, type="html")
>>> for scope in sel.xpath("//div[@itemscope]"):
... print("current scope:", scope.xpath("@itemtype").getall())
... props = scope.xpath(
... """
... set:difference(./descendant::*/@itemprop,
... .//*[@itemscope]/*/@itemprop)"""
... )
... print(f" properties: {props.getall()}")
... print("")
...
current scope: ['http://schema.org/Product']
properties: ['name', 'aggregateRating', 'offers', 'description', 'review', 'review']
current scope: ['http://schema.org/AggregateRating']
properties: ['ratingValue', 'reviewCount']
current scope: ['http://schema.org/Offer']
properties: ['price', 'availability']
current scope: ['http://schema.org/Review']
properties: ['name', 'author', 'datePublished', 'reviewRating', 'description']
current scope: ['http://schema.org/Rating']
properties: ['worstRating', 'ratingValue', 'bestRating']
current scope: ['http://schema.org/Review']
properties: ['name', 'author', 'datePublished', 'reviewRating', 'description']
current scope: ['http://schema.org/Rating']
properties: ['worstRating', 'ratingValue', 'bestRating']
这里我们首先遍历 itemscope 元素,对于每个元素,我们查找所有 itemprops 元素,并排除那些本身在另一个 itemscope 内部的元素。
其他 XPath 扩展
Scrapy 选择器还提供了一个非常需要的 XPath 扩展函数 has-class,对于具有所有指定 HTML 类的节点,它返回 True。
对于以下 HTML
>>> from scrapy.http import HtmlResponse
>>> response = HtmlResponse(
... url="http://example.com",
... body="""
... <html>
... <body>
... <p class="foo bar-baz">First</p>
... <p class="foo">Second</p>
... <p class="bar">Third</p>
... <p>Fourth</p>
... </body>
... </html>
... """,
... encoding="utf-8",
... )
您可以这样使用它
>>> response.xpath('//p[has-class("foo")]')
[<Selector query='//p[has-class("foo")]' data='<p class="foo bar-baz">First</p>'>,
<Selector query='//p[has-class("foo")]' data='<p class="foo">Second</p>'>]
>>> response.xpath('//p[has-class("foo", "bar-baz")]')
[<Selector query='//p[has-class("foo", "bar-baz")]' data='<p class="foo bar-baz">First</p>'>]
>>> response.xpath('//p[has-class("foo", "bar")]')
[]
因此,XPath //p[has-class("foo", "bar-baz")] 大致等同于 CSS p.foo.bar-baz。请注意,在大多数情况下它速度较慢,因为它是一个纯 Python 函数,对每个相关节点都会调用,而 CSS 查询被翻译成 XPath,因此运行效率更高。因此,从性能角度来看,它的用途仅限于那些不容易用 CSS 选择器描述的情况。
Parsel 还通过 set_xpathfunc() 简化了添加您自己的 XPath 扩展。
内置选择器参考
Selector 对象
- class scrapy.Selector(*args: Any, **kwargs: Any)[source]
Selector的实例是对响应的封装,用于选择其内容的特定部分。response是一个HtmlResponse或XmlResponse对象,将用于选择和提取数据。text是一个 Unicode 字符串或 UTF-8 编码文本,用于response不可用的情况。同时使用text和response是未定义行为。type定义选择器类型,可以是"html"、"xml"、"json"、"text"或None(默认)。它被传递给parsel.Selector,其含义在那里定义。然而,当type为None时,在将其传递给parsel.Selector之前,对于XmlResponse对象,它被设置为"xml",否则设置为"html"。注意
JSON 选择器支持需要
parsel1.8.0 或更高版本。使用旧版本时,不支持将type设置为"json"或"text"。- xpath(query: str, namespaces: Mapping[str, str] | None = None, **kwargs: Any) SelectorList[Self][source]
查找匹配 xpath
query的节点,并将结果作为扁平化的SelectorList实例返回。列表元素也实现了Selector接口。query是一个包含要应用的 XPATH 查询的字符串。namespaces是一个可选的prefix: namespace-uri映射(字典),用于为通过register_namespace(prefix, uri)注册的命名空间添加额外前缀。与register_namespace()不同,这些前缀不会保存供将来调用。任何额外的命名参数都可以用于在 XPath 表达式中传递 XPath 变量的值,例如:
selector.xpath('//a[href=$url]', url="http://www.example.com")
注意
为方便起见,此方法可以作为
response.xpath()调用
- css(query: str) SelectorList[Self][source]
应用给定的 CSS 选择器并返回一个
SelectorList实例。query是一个包含要应用的 CSS 选择器的字符串。在后台,CSS 查询使用 cssselect 库翻译成 XPath 查询,并运行
.xpath()方法。注意
为方便起见,此方法可以作为
response.css()调用
- jmespath(query: str, **kwargs: Any) SelectorList[Self][source]
查找匹配 JMESPath
query的对象,并将结果作为扁平化的SelectorList实例返回。列表元素也实现了Selector接口。query是一个包含要应用的 JMESPath 查询的字符串。任何额外的命名参数都会传递给底层的
jmespath.search调用,例如:selector.jmespath('author.name', options=jmespath.Options(dict_cls=collections.OrderedDict))
注意
为方便起见,此方法可以作为
response.jmespath()调用
- re(regex: str | Pattern[str], replace_entities: bool = True) list[str][source]
应用给定的正则表达式,并返回一个包含匹配项的字符串列表。
regex可以是已编译的正则表达式,也可以是使用re.compile(regex)编译为正则表达式的字符串。默认情况下,字符实体引用会被其对应的字符替换(
&和<除外)。将replace_entities设置为False会关闭这些替换。
- re_first(regex: str | Pattern[str], default: None = None, replace_entities: bool = True) str | None[source]
- re_first(regex: str | Pattern[str], default: str, replace_entities: bool = True) str
应用给定的正则表达式并返回第一个匹配的字符串。如果没有匹配项,则返回默认值(如果未提供参数,则为
None)。默认情况下,字符实体引用会被其对应的字符替换(
&和<除外)。将replace_entities设置为False会关闭这些替换。
- register_namespace(prefix: str, uri: str) None[source]
注册给定的命名空间以在此
Selector中使用。如果不注册命名空间,您将无法从非标准命名空间中选择或提取数据。请参阅 XML 响应的选择器示例。
- getall() list[str][source]
序列化并以包含 1 个字符串元素的列表形式返回匹配的节点。
此方法是为了保持一致性而添加到 Selector 中的;它在 SelectorList 中更有用。另请参阅:extract() 和 extract_first()
SelectorList 对象
- class scrapy.selector.SelectorList(iterable=(), /)[source]
SelectorList类是内置list类的子类,它提供了一些额外的方法。- xpath(xpath: str, namespaces: Mapping[str, str] | None = None, **kwargs: Any) SelectorList[_SelectorType][source]
为此列表中的每个元素调用
.xpath()方法,并将其结果扁平化后作为另一个SelectorList返回。xpath与Selector.xpath()中的参数相同namespaces是一个可选的prefix: namespace-uri映射(字典),用于为通过register_namespace(prefix, uri)注册的命名空间添加额外前缀。与register_namespace()不同,这些前缀不会保存供将来调用。任何额外的命名参数都可以用于在 XPath 表达式中传递 XPath 变量的值,例如:
selector.xpath('//a[href=$url]', url="http://www.example.com")
- css(query: str) SelectorList[_SelectorType][source]
为此列表中的每个元素调用
.css()方法,并将其结果扁平化后作为另一个SelectorList返回。query与Selector.css()中的参数相同
- jmespath(query: str, **kwargs: Any) SelectorList[_SelectorType][source]
为此列表中的每个元素调用
.jmespath()方法,并将其结果扁平化后作为另一个SelectorList返回。query与Selector.jmespath()中的参数相同。任何额外的命名参数都会传递给底层的
jmespath.search调用,例如:selector.jmespath('author.name', options=jmespath.Options(dict_cls=collections.OrderedDict))
- get(default: None = None) str | None[source]
- get(default: str) str
返回此列表中第一个元素的
.get()结果。如果列表为空,则返回默认值。
- re(regex: str | Pattern[str], replace_entities: bool = True) list[str][source]
为此列表中的每个元素调用
.re()方法,并将其结果扁平化,作为字符串列表返回。默认情况下,字符实体引用会被其对应的字符替换(
&和<除外)。将replace_entities设置为False会关闭这些替换。
- re_first(regex: str | Pattern[str], default: None = None, replace_entities: bool = True) str | None[source]
- re_first(regex: str | Pattern[str], default: str, replace_entities: bool = True) str
为此列表中的第一个元素调用
.re()方法,并以字符串形式返回结果。如果列表为空或正则表达式不匹配任何内容,则返回默认值(如果未提供参数,则为None)。默认情况下,字符实体引用会被其对应的字符替换(
&和<除外)。将replace_entities设置为False会关闭这些替换。
示例
HTML 响应的选择器示例
这里有一些 Selector 示例,以说明几个概念。在所有情况下,我们都假设已经用一个 HtmlResponse 对象实例化的一个 Selector,如下所示:
sel = Selector(html_response)
从 HTML 响应体中选择所有
<h1>元素,返回一个Selector对象列表(即一个SelectorList对象)sel.xpath("//h1")
从 HTML 响应体中提取所有
<h1>元素的文本,返回一个字符串列表sel.xpath("//h1").getall() # this includes the h1 tag sel.xpath("//h1/text()").getall() # this excludes the h1 tag
遍历所有
<p>标签并打印它们的 class 属性for node in sel.xpath("//p"): print(node.attrib["class"])
XML 响应的选择器示例
这里有一些示例,用以说明使用 XmlResponse 对象实例化的 Selector 对象的概念
sel = Selector(xml_response)
从 XML 响应体中选择所有
<product>元素,返回一个Selector对象列表(即一个SelectorList对象)sel.xpath("//product")
从需要注册命名空间的 Google Base XML feed 中提取所有价格
sel.register_namespace("g", "http://base.google.com/ns/1.0") sel.xpath("//g:price").getall()