python字符串以更健壮的方式解析数字

问题描述 投票:-3回答:1

抓取网站后得到一个字符串。

 '<p class="NewsItemContent" style="font-size: 18px;">;As of March 18, 1999, 
6 p.m. Pacific Daylight Time, there are a total of 70;events and 16;planned  
in this area. This total does not include adjacent cities.</p>'

我该如何解析70、16。只想要一种更强大的方法。措词可能会有所变化,但总共有{};事件和{};计划的。谢谢。

python
1个回答
0
投票

不是一个很干净的解决方案,但是我们要去:

import re

s = ('<p class="NewsItemContent" style="font-size: 18px;">;As of March 18, 1999, '
     '6 p.m. Pacific Daylight Time, there are a total of 70;events and 16;planned  '
     'in this area. This total does not include adjacent cities.</p>')

s = s.split('a total of ')[1]

print(re.findall('\d+', s)[:2])
['70', '16']
© www.soinside.com 2019 - 2024. All rights reserved.