[英]Extracting text between <br> with beautifulsoup, but without next tag
I'm using python + beautifulsoup to try to get the text between the br's. 我正在使用python + beautifulsoup来尝试在br之间获取文本。 The closest I got to this was by using next_sibling in the following manner: 我最接近的是通过以下方式使用next_sibling:
<html>
<body>
</a><span class="strong">Title1</span>
<p>Text1</p>
<br>The Text I want to get<br>
<p>Text I dont want</p>
</body>
</html>
for span in soup.findAll("span", {"class" : "strong"}):
print(span.next_sibling.next_sibling.text)
But this prints: 但这打印:
The Text I want to getText I dont want
So what i want is after the first p, but before the second, but I can't figure out how to extract when there are no real tags, and only just the br's as references. 所以我想要的是在第一个p之后,但在第二个之前,但我无法弄清楚当没有真正的标签时如何提取,只有br作为参考。
I need it to print: 我需要打印:
The Text I want to get
Since the HTML you've provided is broken , the behavior would differ from parser to parser that BeautifulSoup
uses. 由于您提供的HTML已损坏 ,因此BeautifulSoup
使用的解析器与解析器的行为会有所不同。
In case of lxml
parser , BeautifulSoup
would convert the br
tag into a self-closing one: 对于lxml
解析器 , BeautifulSoup
会将br
标记转换为自关闭标记:
>>> soup = BeautifulSoup(data, 'lxml')
>>> print soup
<html>
<body>
<span class="strong">Title1</span>
<p>Text1</p>
<br/>The Text I want to get<br/>
<p>Text I dont want</p>
</body>
</html>
Note that you would need lxml
to be installed. 请注意,您需要安装lxml
。 If it is okay for you - find the br
and get the next sibling: 如果它对你没问题 - 找到br
并获得下一个兄弟:
from bs4 import BeautifulSoup
data = """your HTML"""
soup = BeautifulSoup(data, 'lxml')
print(soup.br.next_sibling) # prints "The Text I want to get"
Also see: 另见:
Using Python Scrapy 使用Python Scrapy
In [4]: hxs.select('//body/text()').extract()
Out[4]: [u'\n', u'\n', u'\n', u'The Text I want to get', u'\n', u'\n']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.