毕业论文着急了?Python疫情数据分析,并做数据可视化展示
阅读原文时间:2023年07月09日阅读:1

采集/确诊人数/新增人数

1. 发送请求

import requests     # 额外安装: 第三方模块

url = 'https://voice.baidu.com/act/newpneumonia/newpneumonia/?from=osari_aladin_banner'
response = requests.get(url)

2. 获取数据 网页源代码

html_data = response.text
# print(response.text)


3. 解析数据
最烦的事情来了,就是提取里面的数据

str_data = re.findall('<script type="application\/json" id="captain-config">\{(.*)\}',html_data)[0]
print(re.findall( '"component":\[(.*)\],',str_data)[0])



用工具去解析一下,在caseList里面就是我们想要的数据了

json_str = re.findall('"component":\[(.*)\],', html_data)[0]     # 字符串
# 字典类型取值, 转类型
json_dict = eval(json_str)
caseList = json_dict['caseList']
for case in caseList:
    area = case['area']                                 # 城市
    curConfirm = case['curConfirm']                     # 当前确诊
    curConfirmRelative = case['curConfirmRelative']     # 新增人数
    confirmed = case['confirmed']                       # 累计确诊
    crued = case['crued']                               # 治愈人数
    died = case['died']                                 # 死亡人数

4. 保存数据

with open('data.csv', mode='a', newline='') as f:
    csv_writer = csv.writer(f)
    csv_writer.writerow([area, curConfirm, curConfirmRelative, confirmed, crued, died])

运行代码,得到数据

完整源码+数据集
各地区确诊人数

china_map = (
    Map()
    .add("现有确诊", [list(i) for i in zip(df['area'].values.tolist(),df['curConfirm'].values.tolist())], "china")
    .set_global_opts(
        title_opts=opts.TitleOpts(title="各地区确诊人数"),
        visualmap_opts=opts.VisualMapOpts(max_=200, is_piecewise=True),
    )
)
china_map.render_notebook()

新型冠状病毒全国疫情地图

cofirm, currentCofirm, cured, dead = [], [], [], []

tab = Tab()

_map = (
    Map(init_opts=opts.InitOpts(theme='dark', width='1000px'))
    .add("累计确诊人数", [list(i) for i in zip(df['area'].values.tolist(),df['confirmed'].values.tolist())], "china", is_map_symbol_show=False,  is_roam=False)
    .set_series_opts(label_opts=opts.LabelOpts(is_show=True))
    .set_global_opts(
        title_opts=opts.TitleOpts(title="新型冠状病毒全国疫情地图",
                                  ),
        legend_opts=opts.LegendOpts(is_show=False),
        visualmap_opts=opts.VisualMapOpts(is_show=True, max_=1000,
                                          is_piecewise=False,
                                          range_color=['#FFFFE0', '#FFA07A', '#CD5C5C', '#8B0000'])
    )
)
tab.add(_map, '累计确诊')

_map = (
    Map(init_opts=opts.InitOpts(theme='dark', width='1000px'))
    .add("当前确诊人数", [list(i) for i in zip(df['area'].values.tolist(),df['curConfirm'].values.tolist())], "china", is_map_symbol_show=False,  is_roam=False)
    .set_series_opts(label_opts=opts.LabelOpts(is_show=True))
    .set_global_opts(
        title_opts=opts.TitleOpts(title="新型冠状病毒全国疫情地图",
                                  ),
        legend_opts=opts.LegendOpts(is_show=False),
        visualmap_opts=opts.VisualMapOpts(is_show=True, max_=100,
                                          is_piecewise=False,
                                          range_color=['#FFFFE0', '#FFA07A', '#CD5C5C', '#8B0000'])
    )
)
tab.add(_map, '当前确诊')

_map = (
    Map(init_opts=opts.InitOpts(theme='dark', width='1000px'))
    .add("治愈人数", [list(i) for i in zip(df['area'].values.tolist(),df['crued'].values.tolist())], "china", is_map_symbol_show=False,  is_roam=False)
    .set_series_opts(label_opts=opts.LabelOpts(is_show=True))
    .set_global_opts(
        title_opts=opts.TitleOpts(title="新型冠状病毒全国疫情地图",
                                  ),
        legend_opts=opts.LegendOpts(is_show=False),
        visualmap_opts=opts.VisualMapOpts(is_show=True, max_=1000,
                                          is_piecewise=False,
                                          range_color=['#FFFFE0', 'green'])
    )
)
tab.add(_map, '治愈')

_map = (
    Map(init_opts=opts.InitOpts(theme='dark', width='1000px'))
    .add("死亡人数", [list(i) for i in zip(df['area'].values.tolist(),df['died'].values.tolist())], "china", is_map_symbol_show=False,  is_roam=False)
    .set_series_opts(label_opts=opts.LabelOpts(is_show=True))
    .set_global_opts(
        title_opts=opts.TitleOpts(title="新型冠状病毒全国疫情地图",
                                  ),
        legend_opts=opts.LegendOpts(is_show=False),
        visualmap_opts=opts.VisualMapOpts(is_show=True, max_=50,
                                          is_piecewise=False,
                                          range_color=['#FFFFE0', '#FFA07A', '#CD5C5C', '#8B0000'])
    )
)
tab.add(_map, '死亡')

tab.render_notebook()

各地区确诊人数与死亡人数情况

\bar = (
    Bar()
    .add_xaxis(list(df['area'].values)[:6])
    .add_yaxis("死亡", df['died'].values.tolist()[:6])
    .add_yaxis("治愈", df['crued'].values.tolist()[:6])
    .set_global_opts(
        title_opts=opts.TitleOpts(title="各地区确诊人数与死亡人数情况"),
        datazoom_opts=[opts.DataZoomOpts()],
        )
)
bar.render_notebook()

## 采集流程
## **一.、明确需求**
> 采集/确诊人数/新增人数> ![请添加图片描述](https://img-blog.csdnimg.cn/b9ae1343217a4761b9968af0dd02e69c.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP54aK54yr54ix5oGw6aWt,size_20,color_FFFFFF,t_70,g_se,x_16)
![请添加图片描述](https://img-blog.csdnimg.cn/c48cd6bc7dd748629b3f754e53943449.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP54aK54yr54ix5oGw6aWt,size_20,color_FFFFFF,t_70,g_se,x_16)
## 二、代码流程 四大步骤
1. 发送请求2. 获取数据 网页源代码3. 解析数据 筛选一些我想用的数据4. 保存数据 保存成表格5. 做数据可视化分析
## 开始代码**1. 发送请求**~import requests     # 额外安装: 第三方模块 url = 'https://voice.baidu.com/act/newpneumonia/newpneumonia/?from=osari_aladin_banner'response = requests.get(url)~**2. 获取数据 网页源代码**~html_data = response.text# print(response.text)~![请添加图片描述](https://img-blog.csdnimg.cn/d1d11611adb0447b9c7e9ffbccc9d859.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCP54aK54yr54ix5oGw6aWt,size_20,color_FFFFFF,t_70,g_se,x_16)**3. 解析数据**最烦的事情来了,就是提取里面的数据~str_data = re.findall('