python - BeautifulSoup Python 得到一个名为“title”的元素
问题描述
所以我有以下HTML:
<span title="总播放数236819" class="view">23.7万播放&nbsp;· </span>
我只想要'236819'。
我创建了 BeautifulSoup 对象和代码:
views = soup.findAll('span', class_ = 'view')
我如何添加到这个/从中删除以获得我想要的位?
谢谢!
解决方案
您可以使用例如re
模块从属性中仅提取数字"title"
:
import re
from bs4 import BeautifulSoup
html_doc = (
"""<span title="总播放数236819" class="view">23.7万播放&nbsp;· </span>"""
)
soup = BeautifulSoup(html_doc, "html.parser")
views = soup.findAll("span", class_="view")
for view in views:
print("".join(re.findall(r"\d+", view["title"]))) # <-- find only digits in "title" attribute
印刷:
236819
推荐阅读
- html - 在 Firefox 中设置按钮的奇怪行为
- c# - C# DateTimeOffset LocalDateTime 与 ToLocalTime
- amazon-web-services - 用于嵌入式设备的 C 语言 WebRTC
- python - 试图调用中断参数。我正在尝试使用选项 5 退出程序,这是我从循环中的中断值
- java - JSOUP 执行意外停止
- c# - 使用asp.net c#在浏览器中打开pdf时如何隐藏打印按钮?
- mysql - 将多个计数与多个 where 子句一起使用
- cen-xfs - WFS_CMD_IDC_READ_RAW_DATA 总是在我的应用程序中返回 WFS_EXEE_IDC_INVALIDMEDIA 事件
- typescript - TypeError 无法读取未定义的属性“实例化”
- javascript - 使用脚本计算 WSO2 6.1.1 中传递日期的下一个日期