python爬取世界疫情數據


import time, json, requests
import csv

#文件名稱
ExcelName = 'E:/hhh.csv'

#當前日期時間戳
number = format(time.time() * 100, '.0f')

url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5&callback=&_=%s' % number
datas = json.loads(requests.get(url=url).json()['data'])

print('更新時間:' + datas['lastUpdateTime'])

#寫入表頭
with open(ExcelName, 'w', encoding='utf-8', newline='') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(["country","confirm","die","cure","time"])



for contry in datas['areaTree']:
    #if contry['name'] == '中國':
        #for province in contry['children']:
            #print(province['name'])
            #with open(ExcelName, 'a', encoding='utf-8', newline='') as csvfile:
                #writer = csv.writer(csvfile)
                #writer.writerow([province['name']])
            #for city in province['children']:
                print(contry['name'], '確診:' + str(contry['total']['confirm']), '死亡:' + str(contry['total']['dead']), '治愈:' + str(contry['total']['heal']))
                # 寫入市的名稱,確診、死亡、治愈的人數
                with open(ExcelName, 'a', encoding='utf-8', newline='') as csvfile:
                    writer = csv.writer(csvfile)
                    writer.writerow([contry['name'], str(contry['total']['confirm']),str(contry['total']['dead']), str(contry['total']['heal']),datas['lastUpdateTime']])

 


免責聲明!

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



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