Python爬虫爬取疫情数据并可视化展示
?這篇文章主要介紹了Python利用爬蟲爬取疫情數(shù)據(jù)并進行可視化的展示,文中的示例代碼講解清晰,對工作或?qū)W習有一定的價值,需要的朋友可以參考一下。編程資料點擊領取
目錄
知識點
開發(fā)環(huán)境
爬蟲完整代碼
導入模塊
分析網(wǎng)站
發(fā)送請求
獲取數(shù)據(jù)
解析數(shù)據(jù)
保存數(shù)據(jù)
數(shù)據(jù)可視化
導入模塊
讀取數(shù)據(jù)
死亡率與治愈率
各地區(qū)確診人數(shù)與死亡人數(shù)情況
知識點
開發(fā)環(huán)境
python 3.8 比較穩(wěn)定版本 解釋器發(fā)行版 anaconda jupyter notebook 里面寫數(shù)據(jù)分析代碼 專業(yè)性
pycharm 專業(yè)代碼編輯器 按照年份與月份劃分版本的
爬蟲完整代碼
導入模塊
| 1 2 3 4 | import requests????? # 發(fā)送網(wǎng)絡請求模塊 import json import pprint??????? # 格式化輸出模塊 import pandas as pd? # 數(shù)據(jù)分析當中一個非常重要的模塊 |
分析網(wǎng)站
先找到今天要爬取的目標數(shù)據(jù)
實時更新:新冠肺炎疫情最新動態(tài)
找到數(shù)據(jù)所在url
發(fā)送請求
| 1 2 | url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5&_=1638361138568' response = requests.get(url, verify=False) |
獲取數(shù)據(jù)
| 1 | json_data = response.json()['data'] |
解析數(shù)據(jù)
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | json_data = json.loads(json_data) china_data = json_data['areaTree'][0]['children'] # 列表 data_set = [] for i in china_data: ????data_dict = {} ????# 地區(qū)名稱 ????data_dict['province'] = i['name'] ????# 新增確認 ????data_dict['nowConfirm'] = i['total']['nowConfirm'] ????# 死亡人數(shù) ????data_dict['dead'] = i['total']['dead'] ????# 治愈人數(shù) ????data_dict['heal'] = i['total']['heal'] ????# 死亡率 ????data_dict['deadRate'] = i['total']['deadRate'] ????# 治愈率 ????data_dict['healRate'] = i['total']['healRate'] ????data_set.append(data_dict) |
保存數(shù)據(jù)
| 1 2 | df = pd.DataFrame(data_set) df.to_csv('data.csv') |
數(shù)據(jù)可視化
導入模塊
| 1 2 | from pyecharts import options as opts from pyecharts.charts import Bar,Line,Pie,Map,Grid |
讀取數(shù)據(jù)
| 1 2 | df2 = df.sort_values(by=['nowConfirm'],ascending=False)[:9] df2 |
死亡率與治愈率
| 1 2 3 4 5 6 7 8 9 10 11 | line = ( ????Line() ????.add_xaxis(list(df['province'].values)) ????.add_yaxis("治愈率", df['healRate'].values.tolist()) ????.add_yaxis("死亡率", df['deadRate'].values.tolist()) ????.set_global_opts( ????????title_opts=opts.TitleOpts(title="死亡率與治愈率"), ????) ) line.render_notebook() |
各地區(qū)確診人數(shù)與死亡人數(shù)情況
| 1 2 3 4 5 6 7 8 9 10 11 | bar = ( ????Bar() ????.add_xaxis(list(df['province'].values)[:6]) ????.add_yaxis("死亡", df['dead'].values.tolist()[:6]) ????.add_yaxis("治愈", df['heal'].values.tolist()[:6]) ????.set_global_opts( ????????title_opts=opts.TitleOpts(title="各地區(qū)確診人數(shù)與死亡人數(shù)情況"), ????????datazoom_opts=[opts.DataZoomOpts()], ????????) ) bar.render_notebook() |
以上就是Python爬蟲爬取疫情數(shù)據(jù)并可視化展示的詳細內(nèi)容。
感謝關(guān)注,為你們準備了編程學習的一套資料,還有相應的代碼,視頻教程都可以獲取,添加Q裙703046414即可獲取。
總結(jié)
以上是生活随笔為你收集整理的Python爬虫爬取疫情数据并可视化展示的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 项目时间管理总结及主要工作清单表
- 下一篇: 基于Python+unittest自动化