Python爬取-腾讯新型冠状病毒疫情时时数据

腾讯新型冠状病毒疫情网址:https://news.qq.com/zt2020/page/feiyan.htm?from=timeline&isappinstalled=0
打开开发者工具,刷新网址进行抓包,发现:https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5&callback=jQuery34107712202448063499_1580707765240&_=1580707765241 里面有我们想要的疫情数据汇总。callback是回调函数,可以尝试清空,最后那个数字表示时间戳,接下来就可以进行抓取了

PS:腾讯接口目前只提供当天的疫情数据,暂时还拿不到以往的时时数据

import time, json, requests
import csv


#文件名称
ExcelName = '2.3疫情日报.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, 'a', encoding='utf-8', newline='') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(['更新时间:' + datas['lastUpdateTime']])

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(city['name'], '确诊:' + str(city['total']['confirm']), '死亡:' + str(city['total']['dead']), '治愈:' + str(city['total']['heal']))
                # 写入市的名称,确诊、死亡、治愈的人数
                with open(ExcelName, 'a', encoding='utf-8', newline='') as csvfile:
                    writer = csv.writer(csvfile)
                    writer.writerow([city['name'], '确诊:' + str(city['total']['confirm']), '死亡:' + str(city['total']['dead']), '治愈:' + str(city['total']['heal'])])

有问题请联系博主:
微信:hrvrap
qq:2580419087

你可能感兴趣的:(Python爬取-腾讯新型冠状病毒疫情时时数据)