import requests
import json
china_url="https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5"
headers={
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36",
"Referer":"https://news.qq.com/"
}
#获取到json数据
response=requests.get(url=china_url,headers=headers).json()
#先把数据改成Python中的字典
data=json.loads(response['data'])
print(response)
#保存数据
with open('./国内疫情.json','w') as f:
#再把字典改成json字符串
f.write(json.dumps(data,ensure_ascii=False,indent=2))
将json数据保存到excel中
import pandas as pd
import json
#将json数据转存到excel中
#读取文件
with open("./国内疫情.json",'r',encoding="utf-8") as f:
data=f.read()
data=json.loads(data)
#获取国内所有数据
chinaAreaDict=data['areaTree'][0]
provinceList=chinaAreaDict['children']
# print(len(provinceList))
china_citylist=[]#{{湖北,武汉},{湖北,襄阳}}
for x in range(len(provinceList)):
province=provinceList[x]['name']
province_List=provinceList[x]['children']
for y in range(len(province_List)):
city=province_List[y]['name']
today=province_List[y]['today']
total=province_List[y]['total']
city_dict={'province':province,"city":city,'today':today,'total':total}
china_citylist.append(city_dict)
chinaTotalData=pd.DataFrame(china_citylist)
#将chinaTotalData中的today和total数据添加到DataFrame中
confirmlist=[]
suspectlist=[]
deadlist=[]
heallist=[]
deadRatelist=[]
healRatelist=[]
for value in chinaTotalData['total'].values.tolist():
confirmlist.append(value['confirm'])
suspectlist.append(value['suspect'])
deadlist.append(value['dead'])
heallist.append(value['heal'])
deadRatelist.append(value['deadRate'])
healRatelist.append(value['healRate'])
chinaTotalData['confirm']=confirmlist
chinaTotalData['suspect']=suspectlist
chinaTotalData['dead']=deadlist
chinaTotalData['heal']=heallist
chinaTotalData['deadRate']=deadRatelist
chinaTotalData['healRate']=healRatelist
# 拆分today列
today_confirmlist=[]
today_confirmCutslist=[]
for value in chinaTotalData['today'].values.tolist():
today_confirmlist.append(value['confirm'])
today_confirmCutslist.append(value['confirmCuts'])
chinaTotalData['today_confirm']=today_confirmlist
chinaTotalData['today_confirmCutslist']=today_confirmCutslist
chinaTotalData.drop(['total','today'],axis=1,inplace=True)
chinaTotalData
#将其保存到excel中
from openpyxl import load_workbook
book=load_workbook('国内疫情.xlsx')
writer=pd.ExcelWriter('国内疫情.xlsx',engine='openpyxl')
writer.book=book
writer.sheets=dict((ws.title,ws) for ws in book.worksheets)
chinaTotalData.to_excel(writer,index=False)
writer.save()
writer.close()