2 回答

TA貢獻(xiàn)2080條經(jīng)驗 獲得超4個贊
假設(shè)可能有更多的 html,我將使用前面的類span與相鄰的兄弟組合器和p類型選擇器來定位適當(dāng)?shù)膒標(biāo)簽
from bs4 import BeautifulSoup as bs
html = '''
<span class="cw-type__h2 Ingredients-title">Ingredients</span>
<p>
THIS IS THE TEXT I WANT TO EXTRACT</p>
'''
soup = bs(html, 'lxml')
print(soup.select_one('.Ingredients-title + p').text.strip())

TA貢獻(xiàn)1798條經(jīng)驗 獲得超3個贊
from bs4 import BeautifulSoup
html = """<span class="cw-type__h2 Ingredients-title">Ingredients</span><p>THIS IS THE TEXT I WANT TO EXTRACT</p>"""
soup = BeautifulSoup(html,'lxml')
print(soup.p.text)
添加回答
舉報