一.数据爬取和数据入库
在使用jsoup爬取数据出现必定问题以后,我改变了方法采用Python来快速爬取疫情数据。python
通过必定时间学习Python相关知识后采用了requests 里的一些方法和 json 格式的转换以及就是数据库的添加操做。mysql
爬取代码以下web


# 爬取腾讯的每日疫情数据 import requests import json import pymysql def get_tencent_data(): """ 爬取目标网站的目标数据 :return: json 类型数据集合 """ #须要爬取的数据网址 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/79.0.3945.130 Mobile Safari/537.36" } r=requests.get(url,headers) res=json.loads(r.text) #第一级转换 json 字符转换为字典 data_all =json.loads(res["data"]) details = [] """ 获取的数据类型以下: lastUpdateTime 最后更新时间 chinaTotal 总数 chinaDayList 历史记录 chinaDayAddList 历史新增记录 areaTree:-name areaTree[0] 中国数据 -today -total -children:-name 省级数据,列表 json类型 -today -total -chilidren:-name 市级数据 列表 -today -total 在上面的url当中 已经没有疫情历史数据 能够在https://view.inews.qq.com/g2/getOnsInfo?name=disease_other 查询 """ update_time=data_all["lastUpdateTime"] data_country=data_all["areaTree"] #lsit集合 47 个国家 data_province =data_country[0]["children"] #中国各省 for pro_infos in data_province: province= pro_infos["name"] #省名 # print(province) for city_infos in pro_infos["children"]: city = city_infos["name"] confirm = city_infos["total"]["confirm"] confirm_add=city_infos["today"]["confirm"] heal= city_infos["total"]["heal"] dead=city_infos["total"]["dead"] details.append([update_time,province,city,confirm,confirm_add,heal,dead]) return details def get_conn(): """ 创建数据库链接 :return: """ conn=pymysql.connect( #本机IP地址 host='127.0.0.1', #数据库用户名 user='root', #密码 password='123456', #须要操做的数据库名称 db='web01', ) #cursor对象 能够进行sql语句执行 和 得到返回值 cursor=conn.cursor() return conn,cursor def close_conn(conn,cursor): """ 关闭链接 :param conn: 链接对象 :param cursor: cursor对象 :return: """ if cursor: cursor.close() if conn: conn.close() def update_yiqingdata(): """ 更新每日数据 :return: """ #获取链接 conn,cursor=get_conn() #获取数据 data=get_tencent_data() #sql语句 对数据库进行操做 sql = "insert into infos(updatetime,province,city,confirm,confirmadd,heal,dead) values(%s,%s,%s,%s,%s,%s,%s)" try: #执行sql语句 cursor.executemany(sql,data) conn.commit() except: conn.rollback() close_conn(conn,cursor) #调用函数 update_yiqingdata()
二.可视化展现
效果以下图:sql
爬取数据后只需将上次的数据查询sql 语句更改一些,并对 Echart 格式进行些许修改便可。数据库
三.学习及实现过程的psp表
日期 | 开始时间 | 结束时间 | 中断时间 | 净时间 | 活动 | 备注 |
3.10 | 15:35 | 17:35 | 10min | 1h50min | 学习jsoup的使用 | 观看视频进行学习json 并对jsoup有了大体了解app |
3.11 | 9:50 | 10:50 | 5min | 55min | 亲自实践使用jsoup | 经过视频案例成功爬取了网页图片 |
3.11 | 13:30 | 15:30 | 0 | 2h | 用jsoup进行数据爬取 | 网页当中js动态生成的网页没法抓取ide 找到使用phantomjs 插件的解决方案函数 对其了解并尝试使用学习 |
3.11 | 16:00 | 17:00 | 0 | 1h | 使用phantomjs插件 | 并未成功爬取到数据 转换思路使用python进行数据爬取 |
3.11 | 19:00 | 22:00 | 30min | 2h30min | 学习python基本语法 以及爬取的相关知识 |
使用python抓取数据,并将给出的示例进行改编 成功实现数据存入数据库,并用Echarts可视化展现 |