要獲取BeautifulSoup中標簽的前后兄弟標簽,可以使用BeautifulSoup提供的find_previous_sibling()
和find_next_sibling()
方法。
例如,如果我們有一個HTML文檔如下:
<html>
<body>
<div id="first">First div</div>
<div id="second">Second div</div>
<div id="third">Third div</div>
</body>
</html>
我們想獲取second
標簽的前后兄弟標簽,可以使用以下代碼:
from bs4 import BeautifulSoup
html = """
<html>
<body>
<div id="first">First div</div>
<div id="second">Second div</div>
<div id="third">Third div</div>
</body>
</html>
"""
soup = BeautifulSoup(html, 'html.parser')
second_div = soup.find(id='second')
previous_sibling = second_div.find_previous_sibling()
next_sibling = second_div.find_next_sibling()
print("Previous sibling:", previous_sibling)
print("Next sibling:", next_sibling)
這樣我們就可以獲取到second
標簽的前一個兄弟標簽和后一個兄弟標簽。