1 回答
TA貢獻(xiàn)1841條經(jīng)驗(yàn) 獲得超3個(gè)贊
如果我將 html 傳遞到代碼中,它會(huì)產(chǎn)生正確的結(jié)果。因此,我假設(shè)傳遞的 HTML 不包含您期望的 HTML 標(biāo)記。您可以通過(guò)運(yùn)行來(lái)檢查這一點(diǎn)print(card.prettify())
這表明您的代碼有效:
from bs4 import BeautifulSoup
html = """<h6 class="var1">
<span class="var2">number</span>
<span class="var3">text1</span>
<span class="var4">text2</span>
</h6>"""
card = BeautifulSoup(html)
print(card.prettify()) # Have a look to see if your html contains the relavant html elements
number=card.find('h6', {'class': 'var1'}).find('span', {'class': 'var2'}).text
print(number)
添加回答
舉報(bào)
