在騰訊新聞和支付寶中我們都能看到疫情數據,但是支付寶的數據獲取難度相對大一些,所以我們獲取的騰訊新聞的數據,鏈接地址:https://news.qq.com/zt2020/page/feiyan.htm?from=timeline&isappinstalled=0
打開該網頁后,我們通過瀏覽器的開發者工具獲取數據(此處我用的edge瀏覽器)。
數據為json格式的,通過查看圖中所有的接口,終於在上圖選中的接口中找到我們想要的數據。
我們想要獲取中國每個城市名稱、確診人數、治愈人數以及死亡人數來繪制疫情地圖。
#!/usr/bin/env python # -*- coding: utf-8 -*- import json import requests #裝了anaconda的可以pip install pyecharts安裝pyecharts # from pyecharts.charts import Map,Geo # from pyecharts import options as opts # from pyecharts.globals import GeoType,RenderType # 繪圖包參加網址https://pyecharts.org/#/zh-cn/geography_charts url="https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5" resp=requests.get(url) html=resp.json() data=json.loads(html["data"]) print(data["lastUpdateTime"]) china=data["areaTree"][0]["children"] print(china) china_total="確診" + str(data["chinaTotal"]["confirm"])+ "疑似" + str(data["chinaTotal"]["suspect"])+ "死亡" + str(data["chinaTotal"]["dead"]) + "治愈" + str(data["chinaTotal"]["heal"]) + "更新日期" + data["lastUpdateTime"] print(china_total) data=[] for i in range(len(china)): data.append([china[i]["name"], china[i]["total"]["confirm"]]) print(data)
運行結果:
這樣我們就獲取到了想要的數據啦,如果想要獲取其他數據,只需要修改該字段名稱即可!