欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python爬蟲爬取疫情數(shù)據(jù)并可視化展示

 更新時(shí)間:2021年12月10日 16:08:01   作者:松鼠愛(ài)吃餅干  
這篇文章主要介紹了Python利用爬蟲爬取疫情數(shù)據(jù)并進(jìn)行可視化的展示,文中的示例代碼講解清晰,對(duì)工作或?qū)W習(xí)有一定的價(jià)值,需要的朋友可以參考一下

知識(shí)點(diǎn)

  1. 爬蟲基本流程
  2. json
  3. requests 爬蟲當(dāng)中 發(fā)送網(wǎng)絡(luò)請(qǐng)求
  4. pandas 表格處理 / 保存數(shù)據(jù)
  5. pyecharts 可視化

開(kāi)發(fā)環(huán)境

python 3.8 比較穩(wěn)定版本 解釋器發(fā)行版 anaconda jupyter notebook 里面寫數(shù)據(jù)分析代碼 專業(yè)性

pycharm 專業(yè)代碼編輯器 按照年份與月份劃分版本的

爬蟲完整代碼

導(dǎo)入模塊

import requests      # 發(fā)送網(wǎng)絡(luò)請(qǐng)求模塊
import json
import pprint        # 格式化輸出模塊
import pandas as pd  # 數(shù)據(jù)分析當(dāng)中一個(gè)非常重要的模塊

分析網(wǎng)站

先找到今天要爬取的目標(biāo)數(shù)據(jù)

https://news.qq.com/zt2020/page/feiyan.htm#/

找到數(shù)據(jù)所在url

發(fā)送請(qǐng)求

url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5&_=1638361138568'
response = requests.get(url, verify=False)

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

json_data = response.json()['data']

解析數(shù)據(jù)

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']
    # 新增確認(rèn)
    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ù)

df = pd.DataFrame(data_set)
df.to_csv('data.csv')

數(shù)據(jù)可視化

導(dǎo)入模塊

from pyecharts import options as opts
from pyecharts.charts import Bar,Line,Pie,Map,Grid

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

df2 = df.sort_values(by=['nowConfirm'],ascending=False)[:9]
df2

死亡率與治愈率

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ù)情況

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ù)并可視化展示的詳細(xì)內(nèi)容,更多關(guān)于Python爬取數(shù)據(jù) 可視化展示的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論