WordPress多语言多站点,子网站建设经验汇报,平面设计软件下载安装,兰州营销型网站建设Python爬虫解析多级嵌套json最近刚接触Python爬虫#xff0c;正好最近肺炎在全国蔓延#xff0c;所以准备从网站爬取肺炎实时数据#xff0c;并解析自己想要的数据。获取json数据网址为 https://m.look.360.cn/events/feiyan爬取网址#xff1a;def main():urlhttps://m.lo…Python爬虫解析多级嵌套json最近刚接触Python爬虫正好最近肺炎在全国蔓延所以准备从网站爬取肺炎实时数据并解析自己想要的数据。获取json数据网址为 https://m.look.360.cn/events/feiyan爬取网址def main():urlhttps://m.look.360.cn/events/feiyanheaders {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,encodingutf-8) as f:f.write(json.dumps(state,indent2,ensure_asciiFalse))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解析格式。输出效果