为什么这个正则表达式不会返回我的价格值?

问题描述 投票:0回答:3
this is the tag im looking to find with the below regex '<span itemprop="price">34.97</span>'
matches = re.findall(r'<span itemprop="price">\$(\d+)</span>', html) 

以上只是尝试过,如果没有$

我希望在这个例子中看到价格34.97,但是当我运行代码时,这里是返回的值(没有返回结果)

最高价格:$ 0最低价格:$ 0 200 []

python
3个回答
0
投票

使用这个正则表达式demo

<span itemprop=\"price\">(\d*\.?\d+)</span>

它考虑小数和数字

如果你真的不在乎跨度之间是什么,这里是它的正则表达式demo2

<span itemprop=\"price\">([^<]+)</span>

随意调整它以满足您的需要,因为re.findall将返回整个范围,因此如果您只需要数字而不是整个范围,则可能需要在此正则表达式中进行前向和后向查找。但这取决于你。


0
投票

你试图捕获34.97

\ d +不占美元和美分之间的时间。

尝试:(\ d +。\ d +)

\d+ (one or more digit)
\. (escaped . character so it captures one period)
\d+ (one or more digits)

https://pythex.org/

是你的朋友!


0
投票

上面的选项和建议都有用,但经过一些测试后我们发现有些情况下标签中没有数值,因此选择了最后一个建议([^ <] +)

这似乎现在抓住了这些情况,并将根据需要进行调整。我们正在构建一个概念蜘蛛,所以这将用于我们的测试和演示功能。

© www.soinside.com 2019 - 2024. All rights reserved.