如何使用 BeautifulSoup 提取中间的代码?

我想从下面的片段中提取文本“这是我想要提取的文本”。有没有人有什么建议?谢谢!


<span class="cw-type__h2 Ingredients-title">Ingredients</span>

<p>

                                THIS IS THE TEXT I WANT TO EXTRACT</p>


侃侃尔雅
浏览 149回答 2
2回答

犯罪嫌疑人X

假设可能有更多的 html,我将使用前面的类span与相邻的兄弟组合器和p类型选择器来定位适当的p标签from bs4 import BeautifulSoup as bshtml = '''<span class="cw-type__h2 Ingredients-title">Ingredients</span><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; THIS IS THE TEXT I WANT TO EXTRACT</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '''soup = bs(html, 'lxml')print(soup.select_one('.Ingredients-title + p').text.strip())

呼如林

from bs4 import BeautifulSouphtml = """<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)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python