采集/确诊人数/新增人数
- 发送请求
- 获取数据 网页源代码
- 解析数据 筛选一些我想用的数据
- 保存数据 保存成表格
- 做数据可视化分析
import requests # 额外安装: 第三方模块 url = 'https://voice.baidu.com/act/newpneumonia/newpneumonia/?from=osari_aladin_banner' response = requests.get(url)
html_data = response.text # print(response.text)
最烦的事情来了,就是提取里面的数据
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'] # 死亡人数
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()