从 class 中获取包含多个元素的文本
Get text from class that has multiple elements
我对 Scrapy 有点陌生。我正在尝试从以下元素中获取文本:
<h1 class="entry-title single-title typesquare_option" itemprop="headline" rel="bookmark">Target Text Here</h1>
但我不知道如何告诉 scrapy“从 H1 获取文本但跳过 itemprop 和 rel 元素”。我可以通过这种方式获取 H1 元素:
response.xpath('//*[contains(@class,"entry-title")]')
哪个returns
[<Selector xpath='//*[contains(@class,"entry-title")]' data='<h1 class="entry-title single-title" ...'>]
所以我越来越接近了,但是如果我试图获取文本,scrapy shell 就会中止。我如何获得文本?我见过的大多数 classes 的例子都有 class 名称旁边的文本,例如:
<h1 class="easy-text">This text is easy to get.</h1>
但是这个在正文之前还有两个东西,itemprop="headline" rel="bookmark",我无法编写正确的 Xpath 来获取正文。我错过了什么?
找到了答案,但如果有人能解释为什么它有效,我将不胜感激。这就是诀窍:
response.xpath('//*[contains(@class,"entry-title single-title")]/text()').get()
不知道之前为什么不行,可能是区域设置的问题(文字不是英文的)。
@Tensigh,首先感谢给个解释的机会。从 html DOM,您会注意到 class, itemprop, rel
不是 h1
的直接子节点。它们被称为属性节点,这些属性节点的紧接节点通常是文本节点,这就是为什么每当我们想要获取属性的值时,我们都会将其紧接在文本节点之后作为值。这里 Target Text Here
是 h1 文本节点之后的直接子节点/紧接节点。由于Target Text Here
是h1的直接子节点,所以我们可以简单的抓取如下:response.xpath('//h1/text()').get()
请参阅scrapy shell
中的output/implementation:
from scrapy.selector import Selector
#我使用 BeautifulSoup 美化了你的 html 树,然后将其粘贴到 scrapy shell 上以便更好地理解。
In [4]: %paste
html_doc="""
<h1 class="entry-title single-title typesquare_option" itemprop="headline" rel="bookmark">
Target Text Here
</h1>
"""
## -- End pasted text --
In [5]: sel = Selector(text=html_doc)
In [6]: sel.getall()
Out[6]: ['<html><body><h1 class="entry-title single-title typesquare_option" itemprop="headline" rel="bookmark">\n Target Text Here\n</h1></body></html>']
In [7]: p = sel.xpath('//h1/text()').get()
In [8]: p
Out[8]: '\n Target Text Here\n'
In [9]: p = sel.xpath('//h1/text()').get().strip()
In [10]: p
Out[10]: 'Target Text Here'
我对 Scrapy 有点陌生。我正在尝试从以下元素中获取文本:
<h1 class="entry-title single-title typesquare_option" itemprop="headline" rel="bookmark">Target Text Here</h1>
但我不知道如何告诉 scrapy“从 H1 获取文本但跳过 itemprop 和 rel 元素”。我可以通过这种方式获取 H1 元素:
response.xpath('//*[contains(@class,"entry-title")]')
哪个returns
[<Selector xpath='//*[contains(@class,"entry-title")]' data='<h1 class="entry-title single-title" ...'>]
所以我越来越接近了,但是如果我试图获取文本,scrapy shell 就会中止。我如何获得文本?我见过的大多数 classes 的例子都有 class 名称旁边的文本,例如:
<h1 class="easy-text">This text is easy to get.</h1>
但是这个在正文之前还有两个东西,itemprop="headline" rel="bookmark",我无法编写正确的 Xpath 来获取正文。我错过了什么?
找到了答案,但如果有人能解释为什么它有效,我将不胜感激。这就是诀窍:
response.xpath('//*[contains(@class,"entry-title single-title")]/text()').get()
不知道之前为什么不行,可能是区域设置的问题(文字不是英文的)。
@Tensigh,首先感谢给个解释的机会。从 html DOM,您会注意到 class, itemprop, rel
不是 h1
的直接子节点。它们被称为属性节点,这些属性节点的紧接节点通常是文本节点,这就是为什么每当我们想要获取属性的值时,我们都会将其紧接在文本节点之后作为值。这里 Target Text Here
是 h1 文本节点之后的直接子节点/紧接节点。由于Target Text Here
是h1的直接子节点,所以我们可以简单的抓取如下:response.xpath('//h1/text()').get()
请参阅scrapy shell
中的output/implementation:
from scrapy.selector import Selector
#我使用 BeautifulSoup 美化了你的 html 树,然后将其粘贴到 scrapy shell 上以便更好地理解。
In [4]: %paste
html_doc="""
<h1 class="entry-title single-title typesquare_option" itemprop="headline" rel="bookmark">
Target Text Here
</h1>
"""
## -- End pasted text --
In [5]: sel = Selector(text=html_doc)
In [6]: sel.getall()
Out[6]: ['<html><body><h1 class="entry-title single-title typesquare_option" itemprop="headline" rel="bookmark">\n Target Text Here\n</h1></body></html>']
In [7]: p = sel.xpath('//h1/text()').get()
In [8]: p
Out[8]: '\n Target Text Here\n'
In [9]: p = sel.xpath('//h1/text()').get().strip()
In [10]: p
Out[10]: 'Target Text Here'