from bs4 import BeautifulSoup
URL = "https://www.worldometers.info/coronavirus/"
r = requests.get(URL)
soup = BeautifulSoup(r.content, 'html5lib')
countHTML = soup.find('div', attrs = {'class':'content-inner'})
for countVar in countHTML.findAll('div', attrs = {'class':'maincounter-number'}):
count = countVar.span
现在变量count
返回:
<span style="color:#aaa">270,069</span>
<span>11,271</span>
<span>90,603</span>
我需要帮忙从这个字符串中提取3个分开的整数,我已经尝试了
count[0]
,但这不是数组,所以它不起作用。String1 = "270,069"
String2 = "11,271"
String3 = "90,603"
接着通过移除逗号将其转换为3个整数
Int1 = 270069
Int2 = 11271
Int3 = 90603
也许正则表达式可以帮上忙?
编辑:
我目前有一个包含
numbers = []
的列表,例如。numbers = """
270069
11271
90603"""
如果我输入numbers[0],那么3个整数会显示为一个值,请问如何去除换行符,并将它们转化为包含3个独立值的列表或数组?
lower_case_with_underscores
风格。 - AMCTraceback (most recent call last): File "/tmp/atom_script_tempfiles/0c9e3b30-6d27-11ea-84a4-095d4171334a", line 2, in
worldometers.info
NameError: name 'worldometers' is not defined
[Finished in 0.069s]
我在 MX-Linux 上使用 Atom,但不知道为什么会出现这种情况! - zero