我正在尝试解析标记<blockquote>
之间的文本。当我输入soup.blockquote.get_text()
时。
我得到了HTML文件中第一个出现的blockquote所需的结果。如何在文件中找到下一个和顺序<blockquote>
标记?也许我只是累了,在文档中找不到它。
示例HTML文件:
<html>
<head>header
</head>
<blockquote>I can get this text
</blockquote>
<p>eiaoiefj</p>
<blockquote>trying to capture this next
</blockquote>
<p></p><strong>do not capture this</strong>
<blockquote>
capture this too but separately after "capture this next"
</blockquote>
</html>
简单的python代码:
from bs4 import BeautifulSoup
html_doc = open("example.html")
soup = BeautifulSoup(html_doc)
print.(soup.blockquote.get_text())
# how to get the next blockquote???
答案 0 :(得分:14)
使用find_next_sibling
(如果不是兄弟,请改用find_next
)
>>> html = '''
... <html>
... <head>header
... </head>
... <blockquote>blah blah
... </blockquote>
... <p>eiaoiefj</p>
... <blockquote>capture this next
... </blockquote>
... <p></p><strong>don'tcapturethis</strong>
... <blockquote>
... capture this too but separately after "capture this next"
... </blockquote>
... </html>
... '''
>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup(html)
>>> quote1 = soup.blockquote
>>> quote1.text
u'blah blah\n'
>>> quote2 = quote1.find_next_siblings('blockquote')
>>> quote2.text
u'capture this next\n'