时间:2022-08-16 12:37:39 | 栏目:Python代码 | 点击:次
python 3.8 比较稳定版本 解释器发行版 anaconda jupyter notebook 里面写数据分析代码 专业性
pycharm 专业代码编辑器 按照年份与月份划分版本的
import requests # 发送网络请求模块 import json import pprint # 格式化输出模块 import pandas as pd # 数据分析当中一个非常重要的模块
先找到今天要爬取的目标数据
https://news.qq.com/zt2020/page/feiyan.htm#/
找到数据所在url
url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5&_=1638361138568' response = requests.get(url, verify=False)
json_data = response.json()['data']
json_data = json.loads(json_data) china_data = json_data['areaTree'][0]['children'] # 列表 data_set = [] for i in china_data: data_dict = {} # 地区名称 data_dict['province'] = i['name'] # 新增确认 data_dict['nowConfirm'] = i['total']['nowConfirm'] # 死亡人数 data_dict['dead'] = i['total']['dead'] # 治愈人数 data_dict['heal'] = i['total']['heal'] # 死亡率 data_dict['deadRate'] = i['total']['deadRate'] # 治愈率 data_dict['healRate'] = i['total']['healRate'] data_set.append(data_dict)
df = pd.DataFrame(data_set) df.to_csv('data.csv')
from pyecharts import options as opts from pyecharts.charts import Bar,Line,Pie,Map,Grid
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()
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="各地区确诊人数与死亡人数情况"), datazoom_opts=[opts.DataZoomOpts()], ) ) bar.render_notebook()