Python实战:抓肺炎疫情实时数据

抓取数据

首先我们确定一个网站,本篇以腾讯疫情实时追踪,按F12,进入开发者工具。【友情提醒:以火狐浏览器为例】
然后,我们打开网络,会发现有一个json格式的链接地址,我们认为这就是我们想要的数据。

地址

参数

响应

深入分析,我们就得到了url地址、请求方法、参数、应答格式等信息。查询参数中,callback是回调函数名,我们可以尝试置空,_应该是以毫秒为单位的当前时间戳。有了这些信息,分分钟就可以抓到数据了。我们先在IDLE中以交互方式抓一下看看效果:

>>> import time, json, requests
>>> url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5&callback=&_=%d'%int(time.time()*1000)
>>> data = json.loads(requests.get(url=url).json()['data'])

只要两行代码,就可以抓到数据了。怎么样,是不是超级简单?我们在来看看数据结构:

>>> data.keys()
dict_keys(['lastUpdateTime', 'chinaTotal', 'chinaAdd', 'isShowAdd', 'showAddSwitch', 'chinaDayList', 'chinaDayAddList', 'dailyHistory', 'wuhanDayList', 'dailyNewAddHistory', 'dailyDeadRateHistory', 'dailyHealRateHistory', 'areaTree', 'articleList'])
>>> d = data['areaTree'][0]['children']
>>> len(d)
34
>>> [item['name'] for item in d]
['湖北', '广东', '河南', '浙江', '湖南', '安徽', '江西', '山东', '江苏', '重庆', '四川', '黑龙江', '北京', '上海', '河北', '福建', '广西', '陕西', '云南', '海南', '贵州', '山西', '天津', '辽宁', '吉林', '甘肃', '新疆', '内蒙古', '宁夏', '香港', '台湾', '青海', '澳门', '西藏']
>>> d[0]['children']
[{'name': '武汉', 'today': {'confirm': 319, 'confirmCuts': 0, 'isUpdated': True}, 'total': {'confirm': 45346, 'suspect': 0, 'dead': 1684, 'deadRate': '3.71', 'showRate': False, 'heal': 6214, 'healRate': '13.70', 'showHeal': True}}, {'name': '孝感', 'today': {'confirm': 17, 'confirmCuts': 0, 'isUpdated': True}, 'total': {'confirm': 3346, 'suspect': 0, 'dead': 94, 'deadRate': '2.81', 'showRate': False, 'heal': 771, 'healRate': '23.04', 'showHeal': True}}, {'name': '黄冈', 'today': {'confirm': 17, 'confirmCuts': 0, 'isUpdated': True}, 'total': {'confirm': 2856, 'suspect': 0, 'dead': 90, 'deadRate': '3.15', 'showRate': False, 'heal': 1274, 'healRate': '44.61', 'showHeal': True}}, {'name': '荆州', 'today': {'confirm': 7, 'confirmCuts': 0, 'isUpdated': True}, 'total': {'confirm': 1517, 'suspect': 0, 'dead': 40, 'deadRate': '2.64', 'showRate': False, 'heal': 531, 'healRate': '35.00', 'showHeal': True}}, {'name': '鄂州', 'today': {'confirm': 5, 'confirmCuts': 0, 'isUpdated': True}, 'total': {'confirm': 1343, 'suspect': 0, 'dead': 39, 'deadRate': '2.90', 'showRate': False, 'heal': 375, 'healRate': '27.92', 'showHeal': True}}, {'name': '随州', 'today': {'confirm': 4, 'confirmCuts': 0, 'isUpdated': True}, 'total': {'confirm': 1287, 'suspect': 0, 'dead': 28, 'deadRate': '2.18', 'showRate': False, 'heal': 386, 'healRate': '29.99', 'showHeal': True}}, {'name': '襄阳', 'today': {'confirm': 3, 'confirmCuts': 0, 'isUpdated': True}, 'total': {'confirm': 1170, 'suspect': 0, 'dead': 24, 'deadRate': '2.05', 'showRate': False, 'heal': 343, 'healRate': '29.32', 'showHeal': True}}, {'name': '黄石', 'today': {'confirm': 7, 'confirmCuts': 0, 'isUpdated': True}, 'total': {'confirm': 974, 'suspect': 0, 'dead': 27, 'deadRate': '2.77', 'showRate': False, 'heal': 339, 'healRate': '34.80', 'showHeal': True}}, {'name': '宜昌', 'today': {'confirm': 4, 'confirmCuts': 0, 'isUpdated': True}, 'total': {'confirm': 895, 'suspect': 0, 'dead': 28, 'deadRate': '3.13', 'showRate': False, 'heal': 250, 'healRate': '27.93', 'showHeal': True}}, {'name': '荆门', 'today': {'confirm': 6, 'confirmCuts': 0, 'isUpdated': True}, 'total': {'confirm': 800, 'suspect': 0, 'dead': 37, 'deadRate': '4.62', 'showRate': False, 'heal': 229, 'healRate': '28.62', 'showHeal': True}}, {'name': '咸宁', 'today': {'confirm': 0, 'confirmCuts': 0, 'isUpdated': True}, 'total': {'confirm': 766, 'suspect': 0, 'dead': 10, 'deadRate': '1.31', 'showRate': False, 'heal': 313, 'healRate': '40.86', 'showHeal': True}}, {'name': '十堰', 'today': {'confirm': 13, 'confirmCuts': 0, 'isUpdated': True}, 'total': {'confirm': 654, 'suspect': 0, 'dead': 2, 'deadRate': '0.31', 'showRate': False, 'heal': 209, 'healRate': '31.96', 'showHeal': True}}, {'name': '仙桃', 'today': {'confirm': 1, 'confirmCuts': 0, 'isUpdated': True}, 'total': {'confirm': 568, 'suspect': 0, 'dead': 19, 'deadRate': '3.35', 'showRate': False, 'heal': 210, 'healRate': '36.97', 'showHeal': True}}, {'name': '天门', 'today': {'confirm': 4, 'confirmCuts': 0, 'isUpdated': True}, 'total': {'confirm': 477, 'suspect': 0, 'dead': 12, 'deadRate': '2.52', 'showRate': False, 'heal': 165, 'healRate': '34.59', 'showHeal': True}}, {'name': '恩施州', 'today': {'confirm': 1, 'confirmCuts': 0, 'isUpdated': True}, 'total': {'confirm': 245, 'suspect': 0, 'dead': 3, 'deadRate': '1.22', 'showRate': False, 'heal': 112, 'healRate': '45.71', 'showHeal': True}}, {'name': '地区待确认', 'today': {'confirm': 220, 'confirmCuts': 0, 'isUpdated': True}, 'total': {'confirm': 220, 'suspect': 0, 'dead': 0, 'deadRate': '0.00', 'showRate': False, 'heal': 0, 'healRate': '0.00', 'showHeal': True}}, {'name': '潜江', 'today': {'confirm': 3, 'confirmCuts': 0, 'isUpdated': True}, 'total': {'confirm': 188, 'suspect': 0, 'dead': 7, 'deadRate': '3.72', 'showRate': False, 'heal': 57, 'healRate': '30.32', 'showHeal': True}}, {'name': '神农架', 'today': {'confirm': 0, 'confirmCuts': 0, 'isUpdated': True}, 'total': {'confirm': 10, 'suspect': 0, 'dead': 0, 'deadRate': '0.00', 'showRate': False, 'heal': 10, 'healRate': '100.00', 'showHeal': True}}]
图示
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容