Python實(shí)戰(zhàn):抓肺炎疫情實(shí)時數(shù)據(jù)

抓取數(shù)據(jù)

首先我們確定一個網(wǎng)站,本篇以騰訊疫情實(shí)時追蹤,按F12,進(jìn)入開發(fā)者工具。【友情提醒:以火狐瀏覽器為例】
然后,我們打開網(wǎng)絡(luò),會發(fā)現(xiàn)有一個json格式的鏈接地址,我們認(rèn)為這就是我們想要的數(shù)據(jù)。

地址

參數(shù)

響應(yīng)

深入分析,我們就得到了url地址、請求方法、參數(shù)、應(yīng)答格式等信息。查詢參數(shù)中,callback是回調(diào)函數(shù)名,我們可以嘗試置空,_應(yīng)該是以毫秒為單位的當(dāng)前時間戳。有了這些信息,分分鐘就可以抓到數(shù)據(jù)了。我們先在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'])

只要兩行代碼,就可以抓到數(shù)據(jù)了。怎么樣,是不是超級簡單?我們在來看看數(shù)據(jù)結(jié)構(gòu):

>>> 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]
['湖北', '廣東', '河南', '浙江', '湖南', '安徽', '江西', '山東', '江蘇', '重慶', '四川', '黑龍江', '北京', '上海', '河北', '福建', '廣西', '陜西', '云南', '海南', '貴州', '山西', '天津', '遼寧', '吉林', '甘肅', '新疆', '內(nèi)蒙古', '寧夏', '香港', '臺灣', '青海', '澳門', '西藏']
>>> 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': '地區(qū)待確認(rèn)', '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': '神農(nóng)架', '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}}]
圖示
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容