Python 爬取每日全国疫情+数据入库+可视化显示

一,数据爬取和数据入库

.本人因为练习需要学习python 进行数据爬取 所以在网上寻找相关的教学视频进行学习

 目前python 用到的只是 requests 里的一些方法和 json 格式的转换 还有就是数据库的添加操作 

编写过程中有问题的就是sql 的执行 我使用的是一个json 集合,但是当像Java web 一样使用sql 语句时出现了问题 :

数据库的表中对多个操作数无法实现同时操作(添加), 故寻找许久找到方法: 

使用 cursor 的excusemany 方法  前一个为sql 语句 后一个为 json 数据集和

cursor.executemany(sql,data)
  1 # 爬取腾讯的每日疫情数据
  2 
  3 import requests
  4 import json
  5 import pymysql
  6 
  7 def get_tencent_data():
  8     """
  9     爬取目标网站的目标数据
 10     :return: json 类型数据集合
 11     """
 12     #需要爬取的数据网址
 13     url="https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5"
 14     headers ={
 15         #用户代理 一个反爬取措施
 16         "user-agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Mobile Safari/537.36"
 17     }
 18 
 19     r=requests.get(url,headers)
 20     res=json.loads(r.text)  #第一级转换  json 字符转换为字典
 21     data_all =json.loads(res["data"])
 22     details = []
 23 
 24     """
 25     获取的数据类型如下:
 26     lastUpdateTime  最后更新时间
 27     chinaTotal 总数
 28     chinaDayList 历史记录
 29     chinaDayAddList 历史新增记录
 30     areaTree:-name           areaTree[0] 中国数据
 31              -today
 32              -total
 33              -children:-name  省级数据,列表  json类型
 34                         -today
 35                         -total
 36                         -chilidren:-name 市级数据 列表
 37                                     -today
 38                                     -total
 39     在上面的url当中 已经没有疫情历史数据 可以在https://view.inews.qq.com/g2/getOnsInfo?name=disease_other 查询
 40     """
 41 
 42     update_time=data_all["lastUpdateTime"]
 43     data_country=data_all["areaTree"]  #lsit集合   47 个国家
 44     data_province =data_country[0]["children"]  #中国各省
 45 
 46     for pro_infos in data_province:
 47         province= pro_infos["name"]   #省名
 48        # print(province)
 49         for city_infos in pro_infos["children"]:
 50             city = city_infos["name"]
 51             confirm = city_infos["total"]["confirm"]
 52             confirm_add=city_infos["today"]["confirm"]
 53             heal= city_infos["total"]["heal"]
 54             dead=city_infos["total"]["dead"]
 55             details.append([update_time,province,city,confirm,confirm_add,heal,dead])
 56     return details
 57 
 58 
 59 def get_conn():
 60     """
 61     建立数据库连接
 62     :return:
 63     """
 64     conn=pymysql.connect(
 65                         #本机IP地址
 66                         host='127.0.0.1',
 67                         #数据库用户名
 68                         user='root',
 69                         #密码
 70                         password='101032',
 71                         #需要操作的数据库名称
 72                         db='db_database06',
 73                         )
 74     #cursor对象 可以进行sql语句执行 和 获得返回值
 75     cursor=conn.cursor()
 76     return conn,cursor
 77 
 78 
 79 def close_conn(conn,cursor):
 80     """
 81     关闭连接
 82     :param conn: 连接对象
 83     :param cursor: cursor对象
 84     :return:
 85     """
 86     if cursor:
 87         cursor.close()
 88     if conn:
 89         conn.close()
 90 
 91 
 92 def update_yiqingdata():
 93     """
 94     更新每日数据
 95     :return:
 96     """
 97     #获取连接
 98     conn,cursor=get_conn()
 99     #获取数据
100     data=get_tencent_data()
101     #sql语句 对数据库进行操作
102     sql = "insert into infos(updatetime,province,city,confirm,confirmadd,heal,dead) values(%s,%s,%s,%s,%s,%s,%s)"
103     try:
104         #执行sql语句
105         cursor.executemany(sql,data)
106         conn.commit()
107     except:
108         conn.rollback()
109     close_conn(conn,cursor)
110 
111 #调用函数
112 update_yiqingdata()
View Code

二,可视化显示:

效果展示:

 

地图代码 请参见: --> 点我惊喜

只是将上次的数据查询sql 语句更改一些,并对 Echart 格式进行些许修改即可

原文地址:https://www.cnblogs.com/cxy0210/p/12492724.html