抓取數(shù)據(jù)
首先我們確定一個網(wǎng)站盗誊,本篇以騰訊疫情實時追蹤,按F12隘弊,進入開發(fā)者工具哈踱。【友情提醒:以火狐瀏覽器為例】
然后梨熙,我們打開網(wǎng)絡开镣,會發(fā)現(xiàn)有一個json格式的鏈接地址,我們認為這就是我們想要的數(shù)據(jù)咽扇。
地址
參數(shù)
響應
深入分析邪财,我們就得到了url地址陕壹、請求方法、參數(shù)树埠、應答格式等信息糠馆。查詢參數(shù)中,callback是回調(diào)函數(shù)名弥奸,我們可以嘗試置空榨惠,_應該是以毫秒為單位的當前時間戳。有了這些信息盛霎,分分鐘就可以抓到數(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ù)結構:
>>> 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ū)待確認', '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}}]
圖示