为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用python beautifulsoup从html中提取文本

如何使用python beautifulsoup从html中提取文本

慕尼黑的夜晚无繁华 2021-08-24 18:36:16
我有一个网页上的以下文字:<dd><p class="Definition"><span class="DefinitionTerm"><df>Example Term</df> </span>Here is the meaning of my term and its description; (<span class="TermLink">définition</span>)</p></dd><dd><p class="Definition"><span class="DefinitionTerm"><df>Example Term 2</df></span>Here is the meaning of my term 2 and its description; (<span class="TermLink">définition</span>)</p></dd><dd><p class="Definition"><span class="DefinitionTerm"><df>Example Term 3</df></span>Here is the meaning of my term 3 and its description; (<span class="TermLink">définition</span>)</p></dd>我正在尝试使用 python beautifulsoup 库来提取定义术语,例如“示例术语”,然后是它的描述。因此,我想看到:“示例术语”、“这是我的术语及其描述的含义”“示例术语 2”、“这是我的术语 2 的含义及其描述”“示例术语 3”、“这是我的 term3 的含义及其描述”
查看完整描述

1 回答

?
PIPIONE

TA贡献1829条经验 获得超9个赞

html = '''<dd><p class="Definition"><span class="DefinitionTerm"><df>Example Term</df> </span>Here is the meaning of my term and its description; (<span class="TermLink">définition</span>)</p></dd><dd><p class="Definition"><span class="DefinitionTerm"><df>Example Term 2</df></span>Here is the meaning of my term 2 and its description; (<span class="TermLink">définition</span>)</p></dd><dd><p class="Definition"><span class="DefinitionTerm"><df>Example Term 3</df></span>Here is the meaning of my term 3 and its description; (<span class="TermLink">définition</span></p></dd>'''


soup = BeautifulSoup(html, 'html.parser')


for each in soup.findAll('p', class_='Definition'):

    print(each.get_text())`


查看完整回答
反对 回复 2021-08-24
  • 1 回答
  • 0 关注
  • 315 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号