Источник страницы, переменная content
, имеет страны в другом порядке, чем в таблице (порядок может изменяться из-за сценария javascript или чего-то подобного).
Таким образом, вы можете просто собрать все данные и упорядочить их в соответствии с общим количеством дел.
import requests,time
from bs4 import BeautifulSoup
# Get the page source and clear it
r = requests.get("https://www.worldometers.info/coronavirus/")
contents = r.content
soup = BeautifulSoup(contents, "html.parser")
table = soup.find("tbody")
countries = table.find_all("tr")
startingIndex = None
# Here we will store the top ten countries values
total=list(range(10))
names=list(range(10))
recovered=list(range(10))
# Compare each "new" country with the current top ten
for index,each in enumerate(countries[8:]):
droebiti_list = each.text.split("\n")
for j in range(10):
if int(droebiti_list[2].replace(',','')) > total[j]:
for jj in reversed(range(j,10)):
recovered[jj]=recovered[jj-1]
names[jj]=names[jj-1]
total[jj]=total[jj-1]
recovered[j]=droebiti_list[6]
names[j]=droebiti_list[1]
total[j]=int(droebiti_list[2].replace(',',''))
break
print(f"{index}){droebiti_list[1]} - {droebiti_list[2]}")
# Print the results
for k in range(10):
print(names[k],'\t\t\t',recovered[k])
интересный вывод:
USA 36,254
Spain 64,727
Italy 35,435
France 27,718
Germany 64,300
UK N/A
China 77,663
Iran 45,983
Turkey 3,957
Belgium 6,707