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 格式進行些許修改即可

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM