疫情数据爬取实战

今天尝试了很多次爬取疫情数据,都没有成功。最后从网上找到了爬取疫情数据的源码,经过阅读才知道自己之前学到的几个简单库的和一些基本的语法都只是python最基础的知识,对抓包等知识并不是很了解,想完全掌握这门语言还需要下很多功夫。

原网址:https://news.qq.com/zt2020/page/feiyan.htm?from=timeline&isappinstalled=0

import time
import json
import csv
import requests
ExcelName = 'C:/Epidemic-data.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(["省份","城市","确诊","死亡","治愈","时间"])
    
for contry in datas['areaTree']:
    if contry['name'] == '中国':
        for province in contry['children']:
            for city in province['children']:
                with open(ExcelName, 'a', encoding='utf-8', newline='') as csvfile:
                    writer = csv.writer(csvfile)
                    writer.writerow([province['name'],city['name'], str(city['total']['confirm']),str(city['total']['dead']), str(city['total']['
heal']),datas['lastUpdateTime']])

爬取结果:

疫情数据爬取实战_第1张图片

 

你可能感兴趣的:(疫情数据爬取实战)