python - 使用 BeautifulSoup 在 DIV 类中的 H 标记中查找部分文本
问题描述
我在 DIV 类内容中有一个 HTML,看起来像
<h2>
<strong>
Brookstone
</strong>
AS20194 Multi-functional Massage Chair
</h2>
我的 Python 代码是
soup.find('div',attrs={'class':'content'}).h2.text
它返回
Brookstone
AS20194 Multi-functional Massage Chair
我应该如何更新代码以使其返回
AS20194 Multi-functional Massage Chair
解决方案
不需要做.extract()
,你可以使用.find_next_sibling()
with 参数text=True
:
from bs4 import BeautifulSoup
txt = '''<h2>
<strong>
Brookstone
</strong>
AS20194 Multi-functional Massage Chair
</h2>'''
soup = BeautifulSoup(txt, 'html.parser')
print(soup.h2.strong.find_next_sibling(text=True))
印刷:
AS20194 Multi-functional Massage Chair
推荐阅读
- html - 如何在angular6中更改onclick上按钮的css?
- c# - Oracle XE 64 位 ODBC 连接与 32 位 c# 应用程序
- wordpress - 自定义 Wordpress 搜索结果
- dart - Flutter在页面中获取导航器参数
- flex-lexer - 如何注释 yacc 中的语法规则和 lex 中的正则表达式匹配规则?
- c++ - 我应该如何在我的 c++ 程序中实现洪水填充功能?
- java - org.openqa.selenium.WebDriverException:浏览器无法启动,在 fluentlium 中测试
- php - 在while循环中删除具有特定ID的行
- ruby-on-rails - Errno::ECONNREFUSED:无法打开到 localhost:4000 的 TCP 连接
- php - ajax php页面上的sql查询