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

抓取數(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}}]
圖示
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末规个,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子姓建,更是在濱河造成了極大的恐慌诞仓,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,590評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件速兔,死亡現(xiàn)場離奇詭異墅拭,居然都是意外死亡,警方通過查閱死者的電腦和手機涣狗,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,157評論 3 399
  • 文/潘曉璐 我一進店門谍婉,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人镀钓,你說我怎么就攤上這事穗熬。” “怎么了丁溅?”我有些...
    開封第一講書人閱讀 169,301評論 0 362
  • 文/不壞的土叔 我叫張陵唤蔗,是天一觀的道長。 經(jīng)常有香客問我窟赏,道長妓柜,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,078評論 1 300
  • 正文 為了忘掉前任饰序,我火速辦了婚禮领虹,結果婚禮上,老公的妹妹穿的比我還像新娘求豫。我一直安慰自己塌衰,他們只是感情好诉稍,可當我...
    茶點故事閱讀 69,082評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著最疆,像睡著了一般杯巨。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上努酸,一...
    開封第一講書人閱讀 52,682評論 1 312
  • 那天服爷,我揣著相機與錄音,去河邊找鬼获诈。 笑死仍源,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的舔涎。 我是一名探鬼主播笼踩,決...
    沈念sama閱讀 41,155評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼亡嫌!你這毒婦竟也來了嚎于?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,098評論 0 277
  • 序言:老撾萬榮一對情侶失蹤挟冠,失蹤者是張志新(化名)和其女友劉穎于购,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體知染,經(jīng)...
    沈念sama閱讀 46,638評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡肋僧,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,701評論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了持舆。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片色瘩。...
    茶點故事閱讀 40,852評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖逸寓,靈堂內(nèi)的尸體忽然破棺而出居兆,到底是詐尸還是另有隱情,我是刑警寧澤竹伸,帶...
    沈念sama閱讀 36,520評論 5 351
  • 正文 年R本政府宣布泥栖,位于F島的核電站,受9級特大地震影響勋篓,放射性物質(zhì)發(fā)生泄漏吧享。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,181評論 3 335
  • 文/蒙蒙 一譬嚣、第九天 我趴在偏房一處隱蔽的房頂上張望钢颂。 院中可真熱鬧,春花似錦拜银、人聲如沸殊鞭。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,674評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽操灿。三九已至锯仪,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間趾盐,已是汗流浹背庶喜。 一陣腳步聲響...
    開封第一講書人閱讀 33,788評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留救鲤,地道東北人久窟。 一個月前我還...
    沈念sama閱讀 49,279評論 3 379
  • 正文 我出身青樓,卻偏偏與公主長得像蜒简,于是被迫代替她去往敵國和親瘸羡。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,851評論 2 361

推薦閱讀更多精彩內(nèi)容