国内外新冠肺炎实时数据爬虫源代码

import json
from prettytable import PrettyTable
import requests
import pandas as pd


def getData_zt2020():
    url = 'https://news.qq.com/zt2020/page/feiyan.htm'
    headers = {
        'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1'
    }
    r = requests.get(url, headers)
    if r.status_code == 200:
        return r.text
    else:
        print("数据拉取失败")
        return ""


def getData_g2():
    url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5'
    headers = {
        'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1'
    }
    req = requests.get(url, headers)
    if req.status_code == 200:
        data = json.loads(json.loads(req.text)['data'])
        return data
    else:
        print("数据拉取失败")
        return ""


def format_list_prettytable(title, province_list):
    table = PrettyTable(title)
    for province in province_list:
        table.add_row(province)
    table.border = True
    return table


def get_china(data):
    """
    lastUpdateTime :最后更新时间
    chinaTotal :累计值
    chinaAdd :每日新增 和 chinaTotal字段一致
    confirm 确诊
    heal 治愈
    dead 死亡
    nowConfirm 现有确诊
    suspect 疑似
    nowSevere 重症
    chinaDayList不提供了,搞得我没法做趋势图
    :param data:
    :return:
    """
    # 新增
    china_add = data['chinaAdd']
    add_confirm = china_add['confirm']  # 新增确诊
    add_dead = china_add["dead"]  # 新增死亡
    add_now_confirm = china_add["nowConfirm"]  # 新增现有确诊
    add_suspect = china_add["suspect"]  # 疑似新增
    add_now_severe = china_add["nowSevere"]  # 新增重症
    add_heal = china_add["heal"]  # 治愈新增
    add_noInfect = china_add["noInfectH5"]  # 无症状新增
    add_importedCase = china_add["importedCase"]  # 境外输入新增
    add_localConfirm = china_add["localConfirmH5"]  # 本土新增确诊

    #累计
    china_total = data['chinaTotal']  # 累计
    total_confirm = china_total['confirm']  # 累计确诊
    total_dead = china_total['dead']  # 累计死亡
    total_heal = china_total['heal']  # 累计治愈
    total_suspect = china_total['suspect']  # 现有疑似
    total_nowSevere = china_total['nowSevere']  # 现有重症
    total_noInfect = china_total['noInfect']  # 现有无症状
    total_importedCase = china_total['importedCase']  # 现有境外输入
    total_localConfirm = china_total['localConfirmH5']  # 本土现有
    title = ["add_localConfirm", "add_noInfect", "add_suspect", "add_confirm", "total_localConfirm", "total_noInfect", "total_suspect"]
    china_list = []
    china_list.append([add_localConfirm, add_noInfect, add_suspect, add_confirm, total_localConfirm, total_noInfect, total_suspect])
    china_table = format_list_prettytable(title, china_list)
    return china_table


def get_all_province(data):
    area_data = data["areaTree"][0]  # 中国数据
    chil_data = area_data['children']
    province_list = []
    for province in chil_data:
        name = province["name"]
        today_confirm = province["today"]["confirm"]  # 今日新增确诊病例数
        today_wzz = province["today"]["wzz_add"]  # 今日新增无症状
        now_confirm = province["total"]["nowConfirm"]  # 现有确诊病例数
        total_confirm = province["total"]["confirm"]   # 累计确诊病例数
        total_wzz = province["total"]["wzz"]   # 累计确诊病例数
        total_confirm = province["total"]["confirm"]   # 累计确诊病例数
        total_heal = province["total"]["heal"]  # 累计治愈
        province_list.append([name, today_confirm, today_wzz, now_confirm, total_confirm, total_heal])
    title = ["name", "today_confirm", "today_wzz", "now_confirm", "total_confirm", "total_heal"]
    province_table = format_list_prettytable(title, province_list)
    return province_table


def parse_jiangsu_province(data, key_province):
    area_data = json.loads(data)["areaTree"]

    data = area_data[0]['children']
    city_list = []

    for province in data:
        name = province['name']
        if name == key_province:
            children_list = province['children']
        for children in children_list:
            city = children["name"]
            today_new = children["today"]["confirm"]
            now_confirm = children["total"]["nowConfirm"]
            total_confirm = children["total"]["confirm"]
            today_heal = children['total']['heal']
            city_list.append([city, today_new, now_confirm, total_confirm, today_heal])
    return city_list


def data_write():
    pass


def data_plot():
    pass


if __name__ == '__main__':
    data = getData_g2()
    china_data = get_china(data)
    print(china_data)
    province_data = get_all_province(data)
    print(province_data)

你可能感兴趣的:(爬虫,python)