Use Python get China epidemic data(historical data)

Demo

Explain

Using Netease public database access epidemic data. And save it in Excel.

Code

# -*- coding:utf-8 -*-
# @Author: Nuanxinqing
# @Time: 2020/12/13 13:45
# @File: 国内数据_Excel.py
# @Software: PyCharm

import requests
import json
import time
import xlwt


# 蜘蛛
def s():
    # 获取现行时间
    now_time = int(round(time.time() * 1000))

    # 数据来源
    url = 'https://c.m.163.com/ug/api/wuhan/app/data/list-total?t=' + str(now_time)

    # 爬虫伪装
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.42'
                      '80.88 Safari/537.36'
    }

    # Start_Spider
    result = requests.get(url, headers=headers)
    data = json.loads(result.text)

    # 解析并保存数据
    js_json(data)


# 解析Json并保存数据
def js_json(data):
    # 保存数据到Excel
    book = xlwt.Workbook(encoding='utf-8', style_compression=0)  # 创建WordBook对象
    sheet = book.add_sheet('海外数据', cell_overwrite_ok=True)  # 创建工作表

    # 创建数据表头
    data_head = ['国家', '日期', '累计确诊', '现有确诊', '当日新增', '累计死亡', '累计治愈']
    for head in range(7):
        sheet.write(0, head, data_head[head])

    # 日期
    data_list_time = []
    for time_for in data['data']['chinaDayList']:
        data_list_time.append(time_for['date'])

    for time_for in range(len(data_list_time)):      # 保存数据
        sheet.write(1 + time_for, 1, data_list_time[time_for])
        sheet.write(1 + time_for, 0, '中国(China)')

    # 累计确诊
    data_list_confirm = []
    for confirm in data['data']['chinaDayList']:
        data_list_confirm.append(confirm['total']['confirm'])

    for confirm in range(len(data_list_confirm)):
        sheet.write(1 + confirm, 2, data_list_confirm[confirm])

    # 现有确诊
    data_list_confirm_total = []
    for today in data['data']['chinaDayList']:
        left_confirm = today['total']['confirm'] - today['total']['heal'] - today['total']['dead']  # 减法获取确诊数
        data_list_confirm_total.append(left_confirm)

    for today in range(len(data_list_confirm_total)):
        sheet.write(1 + today, 3, data_list_confirm_total[today])

    # 当日新增
    data_list_confirm_today = []
    for today in data['data']['chinaDayList']:
        data_list_confirm_today.append(today['today']['confirm'])

    for today in range(len(data_list_confirm_today)):
        sheet.write(1 + today, 4, data_list_confirm_today[today])

    # 累计死亡
    data_list_dead = []
    for dead in data['data']['chinaDayList']:
        data_list_dead.append(dead['total']['dead'])

    for dead in range(len(data_list_dead)):
        sheet.write(1 + dead, 5, data_list_dead[dead])

    # 累计治愈
    data_list_heal = []
    for heal in data['data']['chinaDayList']:
        data_list_heal.append(heal['total']['heal'])

    for heal in range(len(data_list_heal)):
        sheet.write(1 + heal, 6, data_list_heal[heal])

    # 保存数据表
    SaveTime = time.strftime("%Y-%m-%d", time.localtime())
    book.save(SaveTime + '国内数据.xls')


# Start
if __name__ == '__main__':
    s()
THE END
分享
二维码
< <上一篇
下一篇>>