嘿,我目前正在嘗試解析一個網(wǎng)站,我?guī)缀跬瓿闪耍怯幸粋€小問題。我想從 html 代碼中排除內(nèi)部標簽<span class="moto-color5_5"> <strong>Text 1 </strong> <span style="font-size:8px;">Text 2</span></span>我嘗試使用 ...find("span", "moto-color5_5")但這會返回Text 1 Text 2 而不是只返回 Text 1有什么建議么?真誠的:)
1 回答

繁星淼淼
TA貢獻1775條經(jīng)驗 獲得超11個贊
排除內(nèi)部標簽也會排除Text 1,因為它在內(nèi)部標簽<strong>中。
但是,您可以strong在當前湯中找到:
html = """<span class="moto-color5_5">
<strong>Text 1 </strong>
<span style="font-size:8px;">Text 2</span>
</span>
"""
soup = BeautifulSoup(html)
result = soup.find("span", "moto-color5_5").find('strong')
print(result.text) # Text 1
添加回答
舉報
0/150
提交
取消