Sar*_*hak 1 python beautifulsoup
find_previous在特定标签之前提供标签,但我想在<b>标签上方的<table>标签中查找文本。
"
<h2>Hi</h2>
<b>I am here</b>
<b>Output</b>
<h2>Hi</h2>
<table>
.....
</table>
"
Run Code Online (Sandbox Code Playgroud)
预期输出应为Output。我怎样才能做到这一点?
使用您拥有的HTML,以下方法将起作用:
from bs4 import BeautifulSoup
html = """<h2>Hi</h2>
<b>I am here</b>
<b>Output</b>
<h2>Hi</h2>
<table>
.....
</table>"""
soup = BeautifulSoup(html, "html.parser")
print(soup.table.find_previous('b').text)
Run Code Online (Sandbox Code Playgroud)
它将<b>标签显示为:
Output
Run Code Online (Sandbox Code Playgroud)