python - Python,美丽的汤,
标签
问题描述
因此,我查看了堆栈溢出,但似乎找不到问题的答案。如何在 < br > 标签之后获取文本、特定文本?
这是我的代码:
product_review_container = container.findAll("span",{"class":"search_review_summary"})
for product_review in product_review_container:
prr = product_review.get('data-tooltip-html')
print(prr)
这是输出:
Very Positive<br>86% of the 1,013 user reviews for this game are positive.
我只想要这个字符串中的 86%,也只想要 1,013。所以只有数字。但是它不是 int 所以我不知道该怎么做。
以下是文本的来源:
[<span class="search_review_summary positive" data-tooltip-html="Very Positive<br>86% of the 1,013 user reviews for this game are positive.">
</span>]
这是我获取信息的链接:https ://store.steampowered.com/search/?specials=1&page=1
谢谢!
解决方案
你需要在这里使用正则表达式!
import re
string = 'Very Positive<br>86% of the 1,013 user reviews for this game are positive.'
a = re.findall('(\d+%)|(\d+,\d+)',string)
print(a)
output: [('86%', ''), ('', '1,013')]
#Then a[0][0] will be 86% and a[1][1] will be 1,013
其中 \d 是字符串中的任意数字字符,+ 表示至少有 1 个或多个数字。
如果您需要更具体的正则表达式,那么您可以在https://regex101.com中尝试
推荐阅读
- wordpress - 如何在不破坏 WordPress 的情况下使用 .htaccess 向 URL 添加参数
- reactjs - 如何为松弛克隆创建新通道按钮
- go - 如何从 Go 模板调用外部程序
- sql - 插入时的 SSIS 错误 - 维度表中的新主键时出错
- java - 一种递归方法和一种迭代方法的空堆栈
- android - 拦截 NavigationUI.onNavDestinationSelected() 以使用“inclusive = true”使 backstack 弹出
- c - 蓝牙通信延迟大
- mysql - Mysql - 如何合并两个不重复的 json 字符串数组?
- php - PHP datediff 过日问题
- elasticsearch - 未根据 ES 查询中传递的大小参数进行聚合