鱼C论坛

 找回密码
 立即注册
查看: 2569|回复: 10

[已解决]解析json文件获取关键信息

[复制链接]
发表于 2021-1-1 14:35:30 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
https://www.amap.com/service/cityList?version=2020121611
怎么获取这个里面的省会城市
谢谢
最佳答案
2021-1-1 16:35:22
本帖最后由 suchocolate 于 2021-1-1 16:37 编辑
  1. import requests


  2. def main():
  3.     url = 'https://www.amap.com/service/cityList'
  4.     headers = {'user-agent': 'firefox'}
  5.     r = requests.get(url, headers=headers)
  6.     json_data = r.json()['data']['cityData']['provinces']
  7.     for k, v in json_data.items():
  8.         if v['cities']:
  9.             print('省份:{0}, 省会城市:{1}'.format(v['label'], v['cities'][0]['name']))
  10.         else:
  11.             print('直辖市/特区:{}'.format(v['label']))


  12. if __name__ == '__main__':
  13.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-1-1 16:35:22 | 显示全部楼层    本楼为最佳答案   
本帖最后由 suchocolate 于 2021-1-1 16:37 编辑
  1. import requests


  2. def main():
  3.     url = 'https://www.amap.com/service/cityList'
  4.     headers = {'user-agent': 'firefox'}
  5.     r = requests.get(url, headers=headers)
  6.     json_data = r.json()['data']['cityData']['provinces']
  7.     for k, v in json_data.items():
  8.         if v['cities']:
  9.             print('省份:{0}, 省会城市:{1}'.format(v['label'], v['cities'][0]['name']))
  10.         else:
  11.             print('直辖市/特区:{}'.format(v['label']))


  12. if __name__ == '__main__':
  13.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-1 16:41:37 | 显示全部楼层
这个网址是如何来的啊 ,现在已经看不到城市了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-1-2 13:28:52 | 显示全部楼层
wp231957 发表于 2021-1-1 16:41
这个网址是如何来的啊 ,现在已经看不到城市了

好像是个json 点一下网址再刷新
就有了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-3 18:52:51 | 显示全部楼层
我看这里面城市很多,但是在json数据中没有具体区分省会城市的区别,数据都差不多,所以还是需要自己建立一个列表,把区分条件加入进去,比如省会城市名称,或者其他条件
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-3 19:01:43 | 显示全部楼层
也可以用省会城市的areacode来区分
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-3 22:27:36 | 显示全部楼层
你先用浏览器拿到数据,放到JSON格式工具里分析,找找特征。二楼的解答就非常好
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-1-4 13:12:06 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-1-5 13:57:59 | 显示全部楼层
  1. import requests

  2. #获取城市以及对应的adcode
  3. def get_city():
  4.    
  5.     #确定url
  6.     base_url = 'https://www.amap.com/service/cityList?'
  7.     # 发送请求
  8.     response = requests.get(base_url,headers=headers)
  9.     # print(response.text)  
  10.    
  11.     #解析json数据
  12.     json_data = response.json()
  13.     # print(json_data) 得到的json数据放到在线json解析网站中,方便分析结构
  14.    
  15.     #获取adcode
  16.     #热门城市
  17.     city_adcode = []
  18.     for data in json_data['data']['cityData']['hotCitys']:
  19.         city_adcode.append((data['adcode'],data['name'],data['x'],data['y']))
  20.     #其他城市
  21.     #for data in json_data['data']['cityData']['otherCitys']:
  22.         #city_adcode.append((data['adcode'],data['name']))
  23.     return city_adcode
  24.     print(city_adcode)

  25. def get_weather(adcode,city_name,X,Y):
  26.     '''
  27.     获取城市天气
  28.     Query String Parameters
  29.     adcode:500000
  30.     '''
  31.     #基础url
  32.     base_url = 'https://www.amap.com/service/weather?adcode={}'.format(adcode)
  33.     response = requests.get(base_url, headers=headers)  #发送请求,获取响应
  34.     json_data = response.json()  #获取json数据
  35.     #通过得到的json数据,在在线解析网站中解析后,分析其结构,找到要获取的在哪一个字典或者列表列表
  36.     #分层的取出来即可
  37.     if json_data['data']['result']=='true':
  38.         weather = json_data['data']['data'][0]['forecast_data'][0]['weather_name']  #当前天气
  39.         #最大温度
  40.         max_temp = json_data['data']['data'][0]['forecast_data'][0]['max_temp']
  41.         #最小温度
  42.         min_temp = json_data['data']['data'][0]['forecast_data'][0]['min_temp']
  43.         t_weather = json_data['data']['data'][1]['forecast_data'][0]['weather_name']  #明天天气
  44.         #最大温度
  45.         t_max_temp = json_data['data']['data'][1]['forecast_data'][0]['max_temp']
  46.         #最小温度
  47.         t_min_temp = json_data['data']['data'][1]['forecast_data'][0]['min_temp']
  48.         # print(weather, max_temp, min_temp)
  49.         dic = {}
  50.         dic['城市'] = city_name
  51.         dic['adcode'] = adcode
  52.         dic['X'] = X
  53.         dic['Y'] = Y
  54.         dic['天气'] = weather
  55.         dic['温度'] = '{}/{}℃'.format(min_temp,max_temp)
  56.         dic['明日天气'] = t_weather
  57.         dic['明日温度'] = '{}/{}℃'.format(t_min_temp,t_max_temp)
  58.         print(dic)
  59.         
  60.         

  61. def main():
  62.     city_adcode = get_city()
  63.     # print(city_adcode)
  64.     #将每个城市的adcode传给get_weather
  65.     #city_adcode有城市和adcode
  66.     for i in city_adcode:
  67.         adcode = i[0]
  68.         city_name = i[1]
  69.         X = i[2]
  70.         Y = i[3]
  71.         get_weather(adcode,city_name,X,Y)


  72.    
  73. if __name__ == '__main__':
  74.    
  75.     headers = {
  76.         'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537',
  77.         'X-Requested-With': 'XMLHttpRequest',
  78.     }
  79.     main()
复制代码



大佬 能在我这个代码上面把城市改成省会城市吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-5 19:45:42 | 显示全部楼层
本帖最后由 suchocolate 于 2021-1-5 19:50 编辑
私はり 发表于 2021-1-5 13:57
大佬 能在我这个代码上面把城市改成省会城市吗


  1. import requests


  2. def main():
  3.     # 城市字典---> {'北京': [x坐标, y坐标, abcode], '石家庄': [x坐标, y坐标, abcode], ...}
  4.     cities = dict()
  5.     url = 'https://www.amap.com/service/cityList'
  6.     headers = {'user-agent': 'Mobilla'}
  7.     r = requests.get(url, headers=headers)
  8.     json_data = r.json()['data']['cityData']['provinces']
  9.     for k, v in json_data.items():
  10.         # 普通省份
  11.         if v['cities']:
  12.             t = v['cities'][0]
  13.             cities[t['name']] = [t['x'], t['y'], t['adcode']]
  14.         # 直辖市或特区
  15.         else:
  16.             cities[v['name']] = [v['x'], v['y'], k]
  17.     for k, v in cities.items():
  18.         url = 'https://www.amap.com/service/weather?adcode=' + v[2]
  19.         # print(url)
  20.         r = requests.get(url, headers=headers)
  21.         json_data = r.json()['data']['data']
  22.         t_data = json_data[0]['forecast_data'][0]
  23.         m_data = json_data[1]['forecast_data'][0]
  24.         print(f'{k} 今天 天气:{t_data["weather_name"]}, 最高温{t_data["max_temp"]},最低温{t_data["min_temp"]}')
  25.         print(f'     明日 天气:{m_data["weather_name"]}, 最高温{m_data["max_temp"]},最低温{m_data["min_temp"]}')
  26.         print('=' * 100)


  27. if __name__ == '__main__':
  28.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-1-5 22:29:54 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-29 08:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表