import requests
import pandas as pd
import os
import json
def get_page(url,headers):
try:
r=requests.get(url,headers=headers)
r.raise_for_status()
r.encoding=r.apparent_encoding
return r.json()
except Exception as e:
print("error",e)
return ""
def parse_page(data_jason) :
data=data_jason['data']['areaTree'][0]['children']
all_data=[]
for i in data:
province=i['name']
date=i['lastUpdateTime'].split(' ')[0]
for j in i['children']: #注意一定是得在i下的children不要忘了位置
data_one={}
data_one['province']=province
data_one['city']=j['name']
data_one['confirm']=j['total']['confirm']
data_one['suspect']=j['total']['suspect']
data_one['dead']=j['total']['dead']
data_one['heal']=j['total']['heal']
data_one['date']=date
all_data.append(data_one)
result=pd.DataFrame(all_data)
return(result)
def save_file(data_df):
columns=['province','city','confirm','suspect','dead','heal']
data_df.to_excel('疫情数据1.xlsx',index=False,columns=columns)
print("保存成功")
url='https://c.m.163.com/ug/api/wuhan/app/data/list-total?t=316639633292'
headers={'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36'}
data_json=get_page(url,headers)
data_json
result=parse_page(data_json)
save_file(result)