Chr*_*art 3 html python beautifulsoup html-parsing
我有以下html:
<h4>Testing</h4>
<h3>Test</h3>
<h3>Test2</h3>
<h4>Testing2</h4>
Run Code Online (Sandbox Code Playgroud)
如果我<h3>Test2</h3>在变量中引用了元素,我该如何找到<h4>Testing</h4>?在一个之前被引用的元素,而不是之后.
element.previous_sibling
Run Code Online (Sandbox Code Playgroud)
或者,.find_previous_sibling()要明确找到第一个前面的h4标记:
element.find_previous_sibling('h4')
Run Code Online (Sandbox Code Playgroud)
演示:
>>> from bs4 import BeautifulSoup
>>> data = """
... <h4>Testing</h4>
... <h3>Test</h3>
... <h3>Test2</h3>
... <h4>Testing2</h4>
... """
>>> soup = BeautifulSoup(data)
>>> element = soup.find('h3', text='Test')
>>> element.find_previous_sibling('h4')
<h4>Testing</h4>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6013 次 |
| 最近记录: |