查找以前出现的元素

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>?在一个之前被引用的元素,而不是之后.

ale*_*cxe 9

用途.previous_sibling:

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)