• Python:爬取全国各省疫情数据并在地图显示


    代码:

    import requests
    import pymysql
    import json
    from pyecharts import options as opts
    from pyecharts.charts import Map
    
    def create():
    
        # 连接数据库
        db = pymysql.connect(host = 'localhost', user = 'root', password ='258000', database = 'yiqing',charset='utf8')
    
        # 使用 cursor() 方法创建一个游标对象 cursor
        cursor = db.cursor()
    
        # 使用 execute() 方法执行 SQL,如果表存在则删除
        cursor.execute("DROP TABLE IF EXISTS proyiqing")
    
        # 使用预处理语句创建表
        sql = """CREATE TABLE proyiqing (
                Id INT PRIMARY KEY AUTO_INCREMENT,
                update_time varCHAR(255),
                provinse varchar(255),
                today_confirm varchar(255),
                total_confirm varchar(255),
                now_confirm varchar(255),
                total_dead varchar(255),
                total_heal varchar(255))"""
        #update_time,provinse,today_confirm,total_confirm,now_confirm,total_dead,total_heal
        cursor.execute(sql)
    
        #关闭数据库连接
        db.close()
    
    def insert(value):
    
        # 连接数据库
        db = pymysql.connect(host = 'localhost', user = 'root', password ='258000', database = 'yiqing',charset='utf8')
    
        # 使用 execute() 方法执行 SQL,如果表存在则删除
        cursor = db.cursor()
    
        # 使用预处理语句插入数据
        sql = "INSERT INTO proyiqing(update_time,provinse,today_confirm,total_confirm,now_confirm,total_dead,total_heal) VALUES ( %s,%s,%s,%s,%s,%s,%s)"
        #update_time,provinse,today_confirm,total_confirm,now_confirm,total_dead,total_heal
    
        try:
            cursor.execute(sql, value)
            db.commit()
            print('插入数据成功')
        except:
            db.rollback()
            print("插入数据失败")
        db.close()
    
    create()  # 创建表
    
    #目标网站
    url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5'
    headers = {
          'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Mobile Safari/537.36'
        }
    
    #请求资源,获取相应内容
    r = requests.get(url, headers)
    
    # 将响应信息进行json格式化
    res = json.loads(r.text)
    data = json.loads(res['data'])
    
    #世界疫情
    lists = ['截至时间:' + str(data['lastUpdateTime']) + '
    '
            '全国确诊人数:' + str(data['chinaTotal']['confirm']) + '
    '
            '今日新增确诊:' + str(data['chinaAdd']['confirm']) + '
    '
            '全国疑似:' + str(data['chinaTotal']['suspect']) + '
    '
            '今日新增疑似:' + str(data['chinaAdd']['suspect']) + '
    '
            '全国治愈:' + str(data['chinaTotal']['heal']) + '
    '
            '今日新增治愈:' + str(data['chinaAdd']['heal']) + '
    '
            '全国死亡:' + str(data['chinaTotal']['dead']) + '
    '
            '今日新增死亡:' + str( data['chinaAdd']['dead']) + '
    ']
    result = ''.join(lists)
    with open('疫情查询.txt', 'w+', encoding="utf-8") as f:
            f.write(result + '
    ')
    
    #更新时间
    update_time = data['lastUpdateTime']
    
    #保存全国各省现有确诊数据
    province_detals = []
    confirm_detals = []
    
    #省份疫情
    for pro in data['areaTree'][0]['children']:
            provinse = pro["name"]   #省名
            today_confirm = pro["today"]["confirm"]
            total_confirm = pro["total"]["confirm"]
            now_confirm = pro["total"]["nowConfirm"]
            total_dead = pro["total"]["dead"]
            total_heal = pro["total"]["heal"]
            province_detals.append(provinse)
            confirm_detals.append(now_confirm)
            #insert((update_time,provinse,today_confirm,total_confirm,now_confirm,total_dead,total_heal))#存入数据库
    
    #建立一一对应关系
    data_zip = zip(province_detals,confirm_detals)
    data_list = list(data_zip)
    
    #可视化 matplot 和 pyechart
    map = Map(opts.InitOpts(width='1900px',height='800px')).add(series_name="中国疫情分布",
                    data_pair=data_list,#输入数据
                    maptype="china",#地图类型
                    is_map_symbol_show=False#显示标记
    )
    
    #不显示国家名称
    map.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
    
    #设置全局配置项
    map.set_global_opts(title_opts=opts.TitleOpts(title="中国疫情情况"),#设置图标题
                        visualmap_opts=opts.VisualMapOpts(
                            is_piecewise=True,
                            pieces=[
                            {"min":1,"max": 10},
                            {"min": 10, "max": 20},
                            {"min": 20, "max": 30},
                            {"min": 30, "max": 40},
                            {"min": 40, "max": 50},
                            {"min": 50, "max": 60},
                            {"min": 60, "max": 70},
                            {"min": 70, "max": 80},
                            {"min": 80, "max": 90},
                            {"min": 90, "max": 100},
                            {"min": 100}]
                        )) #显示图例
    map.render("中国疫情分布情况.html")

    效果:

  • 相关阅读:
    Java面试题3
    Git 命令
    Flutter 基础控件
    Flutter工程目录
    GitHub简介
    Android Studio 安装 Flutter
    Android 权限管理
    结构型模式-适配器模式
    结构型模式-外观模式
    结构型模式-组合模式
  • 原文地址:https://www.cnblogs.com/linmob/p/14611695.html
Copyright © 2020-2023  润新知