基于python对疫情数据爬取及可视化

先爬取疫情数据:

网站:实时更新:新冠肺炎疫情最新动态

1.打开网页,鼠标右键,点检查,然后点网络,在标头位置可以得到url和User-Agent:

2.在预览的位置可以找需要爬取的数据

 基于python对疫情数据爬取及可视化_第1张图片

# -*-coding:utf-8 -*-
# @Time :  15:38
# @Author: 黄荣津
import time
import requests
import json
import xlwings as xw

headers = {
   'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36'
}
url='https://api.inews.qq.com/newsqa/v1/query/inner/publish/modules/list?modules=statisGradeCityDetail,diseaseh5Shelf'
response = requests.get(url, headers=headers)
# print(response.status_code)  #如果是200则打印成功,其他数字则打印失败
#print(response.json())
json_data = response.json()['data']['diseaseh5Shelf']['areaTree'][0]['children']
# print(json_data)
wb = xw.Book()  # 相当打开excel
sht = wb.sheets('sheet1')  # 在excel中建立一个表

# #A1,B1''' 相当于excel的列名
sht.range('A1').value = '地区'
sht.range('B1').value = '新增确诊'
sht.range('C1').value = '治愈人数'
sht.range('D1').value = '死亡人数'
sht.range('E1').value = '现存人数'
sht.range('F1').value = '累计确诊'
for i in range(34):
    earth_data = json_data[i]
    # print(earth_data)
    province = earth_data['name']
    sht.range(f'A{i + 2}').value = province
    today_confirm = json.dumps(earth_data['today']['confirm'])
    sht.range(f'B{i + 2}').value = today_confirm
    total_dead = json.dumps(earth_data['total']['dead'])
    sht.range(f'D{i + 2}').value = total_dead
    total_heal = json.dumps(earth_data['total']['heal'])
    sht.range(f'C{i + 2}').value = total_heal
    total_nowconfirm=json.dumps(earth_data['total']['nowConfirm'])
    sht.range(f'E{i + 2}').value = total_nowconfirm
    total_confirm = json.dumps(earth_data['total']['confirm'])
    sht.range(f'F{i + 2}').value = total_confirm

    print("地区:" + province, "新增确诊:" + today_confirm,'治愈人数'+total_heal, "死亡:" + total_dead,"现存人数:"+total_nowconfirm, "累计确诊:" + total_confirm)

基于python对疫情数据爬取及可视化_第2张图片

 2.爬取成功,录入excel表格中

3.对数据可视化绘制地图

 

# -*-coding:utf-8 -*-
# @Time :  21:59
# @Author: 黄荣津
# @File : Python读取excel省份数据绘制地图.py
# @Software: PyCharm

import pandas
from pyecharts.charts import Map, Geo
from pyecharts import options as opts
# 导入数据
data = pandas.read_excel(r'data.xlsx', sheet_name='Sheet1')
# 将数据转换为二元的列表
list1 = list(zip(data['地区'],data['现存人数']))

# 创建一个地图对象
map_1 = Map()  # 对全局进行设置
map_1.set_global_opts(
    # 设置标题
    title_opts=opts.TitleOpts(title="全国疫情地图"), visualmap_opts=opts.VisualMapOpts(is_piecewise=True,
         pieces=[
             {"min": 1000, "label": '>10000人',
              "color": "#6F171F"},
             # 不指定 max,表示 max 为无限大(Infinity)

             {"min": 5000, "max": 10000,
              "label": '5000-10000人',
              "color": "#C92C34"},

             {"min": 1000, "max": 4990,
              "label": '1000-4990人',
              "color": "#E35B52"},

             {"min": 100, "max": 990,
              "label": '100-990人',
              "color": "#F39E86"},

            {"min": 10, "max": 99,
              "label": '10-99人',
              "color": "pink"},
             {"min": 1, "max": 9,
              "label": '1-9人',
              "color": "#FDEBD0"}]))

# 使用add方法添加地图数据与地图类型
map_1.add("现有确诊人数",list1, maptype="china",is_map_symbol_show = False)  #不显示红点

# 地图创建完成后,通过render()方法可以将地图渲染为html
map_1.render('全国各省现有确诊人数疫情地图.html')


基于python对疫情数据爬取及可视化_第3张图片

 基于python对疫情数据爬取及可视化_第4张图片

 基于python对疫情数据爬取及可视化_第5张图片

你可能感兴趣的:(笔记,python)