最近我又又又在家里居家办公了
闲来无事,用python采集一下 再做个可视化
源码、资料素材电子书点击这里
1. 爬虫基本流程
2. requests 发送请求
3. re 正则表达式
4. json 结构化数据解析
5. pyecharts 可视化
采集 确诊人数 新增人数 数据所在位置:
地址
发送请求 地址
获取数据 网页源代码
解析数据 筛选一些我想用的数据
保存数据 保存成表格
做数据可视化分析
import requests # 额外安装: 第三方模块
import re # 内置模块
import csv
with open('data.csv', mode='a', newline='') as f:
csv_writer = csv.writer(f)
csv_writer.writerow(['area', 'curConfirm', 'curConfirmRelative', 'confirmed', 'crued', 'died'])
url = 'https://voice.baidu.com/act/newpneumonia/newpneumonia/?from=osari_aladin_banner'
response = requests.get(url)
# : 请求成功意思
html_data = response.text
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'] # 死亡人数
print(area, curConfirm, curConfirmRelative, confirmed, crued, died)
# 乱码 encoding="utf-8" / encoding="gbk"
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(