2019ncov的世界各国数据保存到excel表格。
最后数据展示:
数据一共包含date、confirm、confirm_add、dead和heal,如下:
每一个sheet都是以各国名称命名,数据也是到最新更新的那天。
代码展示:
import requests
import pandas as pd
import time
import datetime
def spider():
url = 'https://api.inews.qq.com/newsqa/v1/automation/foreign/country/ranklist' ###数据获取网址
response = requests.post(url).json()
data = response.get('data')
# print(data)
global_data = pd.DataFrame(data)
global_data['name'] = global_data['name']
global_data['confirm'] = global_data['confirm']
global_data['dead'] = global_data['dead']
global_data['heal'] = global_data['heal']
global_data = global_data[['name','confirm','dead','heal']] ###此处获取的是各国最新疫情数据
print(global_data)
####给你们展示下前几行的print:
#### name confirm dead heal
####0 美国 582594 23649 44308
####1 西班牙 169628 17628 64727
####2 意大利 159516 20465 35435
####3 法国 137877 14986 28001
####4 德国 130072 3194 68200
writer = pd.ExcelWriter( 'global_datalist.xls') ###开启写入excel
for i in global_data['name']: ###获取各国名
url_city = 'https://api.inews.qq.com/newsqa/v1/automation/foreign/daily/list?country=' + str(i) ###更换各国网址
response = requests.post(url_city).json()
data = response.get('data')
global_list = pd.DataFrame(data)
global_list['date'] = global_list['date']
global_list['confirm'] = global_list['confirm']
global_list['confirm_add'] = global_list['confirm_add']
global_list['dead'] = global_list['dead']
global_list['heal'] = global_list['heal']
global_list = global_list[['date', 'confirm', 'confirm_add', 'dead', 'heal']] ###表头
global_list.to_excel(writer,sheet_name=i) ###以各国名称命名每个sheet
time.sleep(100)
writer.save()
writer.close() ###数据保存完毕
###定时器
def main(h=23, m=13): ###设置在23点13分自动运行代码
while True:
while True:
now = datetime.datetime.now()
if now.hour==h and now.minute==m:
break
time.sleep(60)
spider()
print(now,'Task finished')
if __name__== '__main__':
main()