Parking of website table
-
I can't figure out how to evaporate the table on this website.
import requests from bs4 import BeautifulSoup
URL = 'https://coronavirus-graph.ru/rossiya/moskva'
def get_html(url, params=None):
r = requests.get(url, params=params)
return rdef get_content(html):
soup = BeautifulSoup(html, 'html.parser')
items = soup.find_all('div', class_='stat_table_box')
people = []
for item in items:
people.append({
'title': item.find('tbody') .get_text()
})
print(people)def parse():
html = get_html(URL)
if html.status_code == 200:
get_content(html.text)
else:
print('Error')
-
If the use of BeautifulSoup is not in principle, pandas can be used, which is what is meant for (and not only). And the code is straight in two lines.
>>> import pandas >>> ds = pandas.read_html("https://coronavirus-graph.ru/rossiya/moskva") >>> ds [ Дата Заражений всего Умерло всего Летальность Выздоровело всего Болеющих 0 4 ноября 18433626305 3172897 0.05% 16186246633 ➜193 010-425 1 3 ноября 18370576827 3163195 0.05% 16119916628 ➜193 435+104 2 2 ноября 18302305736 3153698 0.05% 16053636359 ➜193 331-721 3 1 ноября 18244947103 3143896 0.05% 15990045257 ➜194 052+1750 4 31 октября 18173917603 3134294 0.05% 15937475050 ➜192 302+2459 .. ... ... ... ... ... ... 594 20 марта 13133 — — 1 ➜130+33 595 19 марта 9812 — — 1 ➜97+12 596 18 марта 8631 — — 1 ➜85+31 597 17 марта 551 — — 1 ➜54+1 598 16 марта 5420 — — 1 ➜53+20
[599 rows x 6 columns]]
And now all these data are in a convenient structure and can do anything with them. And if you read https://pandas.pydata.org/docs/user_guide/10min.html#min It'll be wonderful.