Python疫情数据下载,并保存成Excel

Python疫情数据下载,并保存成Excel

import requests
import json
import os
import openpyxl


res = requests.get(url)
# 模拟浏览器请求
headers = {
 'user-agent': 'text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01'

}

def get_data(provinceName):
    data_url = "https://api.inews.qq.com/newsqa/v1/query/pubished/daily/list?province=" + provinceName
    # 腾讯疫情数据发送http请求
    res3 = requests.get(data_url, headers=headers).text
    # json数据解析
    data = json.loads(res3)['data']
    print(data)

    yqsheet = yq.create_sheet(provinceName)
    #定义表头
    head = ['日期', '新增','累计','死亡', '治愈']
    yqsheet.append(head)
    for row in range(0, len(data)):
        yqsheet.cell(row + 2, 1, data[row]['date'])
        yqsheet.cell(row + 2, 3, data[row]['confirm'])
        yqsheet.cell(row + 2, 2, data[row]['confirm_add'])
        yqsheet.cell(row + 2, 4, data[row]['dead'])
        yqsheet.cell(row + 2, 5, data[row]['heal'])
    return
for p in range(0,len(province_list)):
    province_data.append(province_list[p]['provinceName'])

for s in range(0,len(province_data)):
    get_data(province_data[s])

print(province_data)

yq.remove_sheet(yq.get_sheet_by_name('Sheet'))
yq.save('疫情数据.xlsx')









你可能感兴趣的:(python,json,腾讯)