2 回答
犯罪嫌疑人X
TA贡献2080条经验 获得超4个赞
假设可能有更多的 html,我将使用前面的类span与相邻的兄弟组合器和p类型选择器来定位适当的p标签
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贡献1798条经验 获得超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)
添加回答
举报
0/150
提交
取消