Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Dears, I need to transform the Covid hospitalisation json data from the government webpage: https://onemocneni-aktualne.mzcr.cz/covid-19#panel3-hospitalization

I inspect the webpage and identified the table in the below-showed html code.

I used the following Python code and got the outcome below:

import bs4 as bs
import urllib.request
import json

source = urllib.request.urlopen("https://onemocneni-aktualne.mzcr.cz/covid-19#panel3-hospitalization")
soup = bs.BeautifulSoup(source)
js_test = soup.find("div", id="js-hospitalization-table-data")

#Convert to JSON object
jsonData = json.loads(js_test.attrs["data-table"])   
print (jsonData['body'])

Thank you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
186 views
Welcome To Ask or Share your Answers For Others

1 Answer

The data you want is in JSON format, you can convert it to a Python dictionary (dict) and get the data under the body key using the built-in json module.

import json
import bs4 as bs
import urllib.request

source = urllib.request.urlopen(
    "https://onemocneni-aktualne.mzcr.cz/covid-19#panel3-hospitalization"
)
soup = bs.BeautifulSoup(source, "html.parser")

json_data = json.loads(
    soup.find("div", id="js-hospitalization-table-data")["data-table"]
)

print(type(json_data))
print(*json_data["body"])

Output (partial):

<class 'dict'>
['01.03.2020', 0, 0, 0, 0, 0] ['02.03.2020', 0, 0, 0, 0, 0] ... ['20.12.2020', 4398, 588, 0.1337, 34796, 0.7152]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...