我目前正在尝试从网页中抓取一些数据。我需要的数据<meta>
在html源代码的标签内。使用BeautifulSoup抓取数据并将其保存到String没问题。
字符串包含我要提取的2个数字。这些数字中的每一个(评论评分为1-100)都应分配给不同的变量以进行进一步处理。
test_str = "<meta content=\"Overall Rating: 79/100 ... Some Info ... Score: 86/100 \"/>"
第一个值是79/100
,第二个值是86/100
,但是我只需要79
和86
。到目前为止,我已经创建了一个正则表达式搜索来查找这些值,然后.replace("/100")
进行清理。
但是,使用我的代码,我只会得到第一个正则表达式搜索匹配的值79
。我尝试使用来获取第二个值,m.group(1)
但是它不起作用。
我想念什么?
test_str = "<meta content=\"Overall Rating: 79/100 ... Some Info ... Score: 86/100 \"/>"
m = re.search("../100", test_str)
if m:
found = m.group(0).replace("/100","")
print found
# output -> 79
谢谢你的帮助。
最好的祝福!
test_str = "<meta content=\"Overall Rating: 79/100 ... Some Info ... Score: 86/100 \"/>"
m = re.findall('(\d+(?=\/100))', test_str)
# m = ['79', '86']
我更改为..
,/d+
因此您可以搜索1位数字或2位数字
我也使用积极的前瞻(?=...)
,因此.replace
变得不必要
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句