2 回答

TA贡献1829条经验 获得超4个赞
您可以获得sorting_1课程,因为它不存在于页面源代码中。您已找到表中的所有行,然后从所需的列中读取信息。
因此,要获取每个国家/地区的总案例,您可以使用以下代码:
import requests
import bs4
res = requests.get('https://www.worldometers.info/coronavirus/')
soup = bs4.BeautifulSoup(res.text, 'lxml')
country_cases = soup.find_all('td', {'class': 'sorting_1'})
rows = soup.select('table#main_table_countries_today tr')
for row in rows[8:18]:
tds = row.find_all('td')
print(tds[1].text.strip(), '=', tds[2].text.strip())

TA贡献1883条经验 获得超3个赞
这些类似乎sorting_X是由 javascript 添加的,因此它们不存在于原始 html 中。
但是,该表确实存在,因此我建议循环遍历类似于此的表行:
table_rows = soup.find("table", id="main_table_countries_today").find_all("tr")
for row in table_rows:
name = "unknown"
# Find country name
for td in row.find_all("td"):
if td.find("mt_a"): # This kind of link apparently only exists in the "name" column
name = td.find("a").text
# Do some more scraping
警告,我有一段时间没有喝汤了,所以这可能不是 100% 正确。你明白了。
添加回答
举报