python爬取疫情數據


具體要求:

        從網頁爬取全國疫情分布情況,讀取入庫結合圖形化展示。

思路:

  • 抓取
  • 分析
  • 存儲

  在項目導入requests和PyMysql包;

  發送請求,並打印數據狀態碼;

  分析爬取到的數據:

 

 

     name是國家/省/市的名字;id為地區編號;lastUpdateTime是最后更新時間;total為累計數據;today為今天新增數據;confirm、suspect、heal、dead分別為確診、疑似、治愈、死亡人數;

  建表為:

 

 

   連接並將數據存入數據庫:

conn = pymysql.connect(
        host='localhost',  # 我的IP地址
        port=3306,  # 不是字符串不需要加引號。
        user='root',
        password='123456',
        db='test',
        charset='utf8'
    )

    cursor = conn.cursor()  # 獲取一個光標
    id = 0;
    for dict in province_list:
        sql = 'insert into yiqing_copy1 (province,date,total_confirm,total_suspect,total_heal,total_dead,today_confirm,today_suspect,today_heal,today_dead,id) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);'
        province=dict['name']
        date = dict['lastUpdateTime']
        total_confirm=dict['total']['confirm']
        total_suspect=dict['total']['suspect']
        total_heal=dict['total']['heal']
        total_dead=dict['total']['dead']
        today_confirm=dict['today']['confirm']
        today_suspect=dict['today']['suspect']
        today_heal=dict['today']['heal']
        today_dead=dict['today']['dead']
        id=id+1
        sys.stdout.write( dict['name'] + '  ')
        cursor.execute(sql, [province, date,total_confirm, total_suspect,total_heal,total_dead,today_confirm,today_suspect,today_heal,today_dead,id])
View Code

完整代碼:

import requests
import time, json
import sys;
import pymysql

def get_wangyi_request():
    url = 'https://c.m.163.com/ug/api/wuhan/app/data/list-total'

    headers = {
        'accept': '*/*',
        'accept-encoding': 'gzip,deflate,br',
        'accept-language': 'en-US,en;q=0.9,zh-CN;q = 0.8,zh;q = 0.7',
        'origin': 'https://wp.m.163.com',
        'referer': 'https://wp.m.163.com/',
        'sec-fetch-dest': 'empty',
        'sec-fetch-mode': 'cors',
        'sec-fetch-site': 'same-ite',
        'user-agent': 'Mozilla/5.0(WindowsNT10.0;Win64;x64) AppleWebKit/37.36 (KHTML, likeGecko) Chrome/82.0.4056.0 Safari/537.36 Edg/82.0.432.3'
    }

    result = requests.get(url, headers=headers)
    return result


def print_mess1(string: str, dict1total: dict):
    sys.stdout.write(string + '確診: ' + str(dict1total['confirm'] if dict1total['confirm'] != None else 0))
    sys.stdout.write(' ')
    sys.stdout.write(string + '疑似: ' + str(dict1total['suspect'] if dict1total['suspect'] != None else 0))
    sys.stdout.write(' ')
    sys.stdout.write(string + '治愈: ' + str(dict1total['heal'] if dict1total['heal'] != None else 0))
    sys.stdout.write(' ')
    sys.stdout.write(string + '死亡: ' + str(dict1total['dead'] if dict1total['dead'] != None else 0))


if __name__ == '__main__':
    result = get_wangyi_request()

    json_str = json.loads(result.text)['data']
    # print(json_str.keys())
    # dict_keys(['chinaTotal', 'chinaDayList', 'lastUpdateTime', 'areaTree'])

    print(json_str['lastUpdateTime'])
    province_list = json_str['areaTree'][0]['children']
    # 每個省份包含如下的鍵
    # dict_keys(['today', 'total', 'extData', 'name', 'id', 'lastUpdateTime', 'children'])

    conn = pymysql.connect(
        host='localhost',  # 我的IP地址
        port=3306,  # 不是字符串不需要加引號。
        user='root',
        password='123456',
        db='test',
        charset='utf8'
    )

    cursor = conn.cursor()  # 獲取一個光標
    id = 0;
    for dict in province_list:
        sql = 'insert into yiqing_copy1 (province,date,total_confirm,total_suspect,total_heal,total_dead,today_confirm,today_suspect,today_heal,today_dead,id) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);'
        province=dict['name']
        date = dict['lastUpdateTime']
        total_confirm=dict['total']['confirm']
        total_suspect=dict['total']['suspect']
        total_heal=dict['total']['heal']
        total_dead=dict['total']['dead']
        today_confirm=dict['today']['confirm']
        today_suspect=dict['today']['suspect']
        today_heal=dict['today']['heal']
        today_dead=dict['today']['dead']
        id=id+1
        sys.stdout.write( dict['name'] + '  ')
        cursor.execute(sql, [province, date,total_confirm, total_suspect,total_heal,total_dead,today_confirm,today_suspect,today_heal,today_dead,id])
    print()

    conn.commit()


    cursor.close()
    conn.close()

數據庫:

 


免責聲明!

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



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