1、實時數據網址
新型冠狀病毒肺炎疫情實時追蹤

2、抓包
使用瀏覽器自帶的抓包功能,找到返回的數據,確定格式是json格式。
3、返回json數據地址

4、處理json數據
通過傳入的網址,處理json數據,返回字典對象
def getData(url):
html=requests.get(url)
#獲取返回的json數據
data_json=html.json()
data_dict=json.loads(data_json['data'])
return data_dict
字典對象格式如下:
5、獲取數據獲取時間
data_dict=getData('
https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5')
#新冠數據獲取時間
print("數據獲取時間:{}".format(data_dict['lastUpdateTime'] ))


6、獲取各國新冠數據
分析字典中鍵和值,
代碼如下
def getAllCountry():
for area in data_dict['areaTree']:
print("{}新冠詳情\r\n確診{}\r\n治愈{}\r\n死亡{}\r\n疑似{}".format(area['name'], \
area['total']['confirm'],\
area['total']['heal'],\
area['total']['dead'],\
area['total']['suspect'],\
))
執行結果
7、獲取中國各省新冠數據
分析字典中鍵和值
代碼如下
def getChinaProvince():
for province in data_dict['areaTree'][0]['children']:
print("{}新冠詳情\r\n確診{}\r\n治愈{}\r\n死亡{}\r\n疑似{}".format(province['name'], \
province['total']['confirm'],\
province['total']['heal'],\
province['total']['dead'],\
province['total']['suspect'],\
))
執行結果

8、獲取中國各市新冠數據
分析字典中鍵和值

代碼如下
def getChinaCity():
#for i in len(data_dict['areaTree'][0]['children']):
for i in range(0, len(data_dict['areaTree'][0]['children'])):
print("\r\n{}省".format(data_dict['areaTree'][0]['children'][i]['name']))
for city in data_dict['areaTree'][0]['children'][i]['children']:
print("{}新冠詳情\r\n確診{}\r\n治愈{}\r\n死亡{}\r\n疑似{}".format(city['name'], \
city['total']['confirm'],\
city['total']['heal'],\
city['total']['dead'],\
city['total']['suspect'],\
))
運行結果如下:
