Python爬虫解析多级嵌套json
最近刚接触Python爬虫,正好最近肺炎在全国蔓延,所以准备从网站爬取肺炎实时数据,并解析自己想要的数据。获取json数据网址为 https://m.look.360.cn/events/feiyan
爬取网址:
def main():
url='https://m.look.360.cn/events/feiyan'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36 QIHU 360EE'}
parse_page(url)
解析并保存json格式文件:
state = json.loads(response.content)
with open("feiyan.json", "w",encoding='utf-8') as f:
f.write(json.dumps(state,indent=2,ensure_ascii=False))
print("保存成功")
获取json文件后发现为嵌套格式,具体格式大家可以打开上方网址查看:
{
"data":[
{
"data":{省份}
"citys":[
{城市}{}.....
]
}
{
"data":{省份}
"citys":[
{城市}{}.....
]
}
"country":[
{其他国家}
]
]
}
解析数据
因为我只想获取各个城市的名称、感染人数、治愈人数和死亡人数,所以需要嵌套循环来获取并逐个输出。代码如下:
省级数据获取:
provincename = state['data'][i]['data']['provinceName']
confirmedCount = state['data'][i]['data']['confirmedCount']
curedCount = state['data'][i]['data']['curedCount']
deadCount = state['data'][i]['data']['deadCount']
城市数据获取:
cityName = state['data'][i]['cities'][j]['cityName']
diagnosed = state['data'][i]['cities'][j]['diagnosed']
cured = state['data'][i]['cities'][j]['cured']
died = state['data'][i]['cities'][j]['died']
国外城市数据获取:
countryname = state['country'][c]['provinceName']
diagnosed = state['country'][c]['diagnosed']
cured = state['country'][c]['cured']
died = state['country'][c]['died']
以上就是嵌套json解析格式。
输出效果: