| 
 | 
 
10鱼币 
 本帖最后由 私はり 于 2021-1-4 17:37 编辑  
 
这是题目要求: 
编写程序从 
https://www.amap.com/service/cityList?version=2020121611获得省会城市的坐标轴(x和y)和adcode 
然后根据城市adcode编码组合到以下网址 
https://www.amap.com/service/weather?adcode=510400 
获得当前天气气候和明后两天的天气气候 
 
我自己在网上搜了代码并修改了些,剩下的就不会做了 
 
- import requests
 
  
- #获取城市以及对应的adcode
 
 - def get_city():
 
 -     
 
 -     #确定url
 
 -     base_url = 'https://www.amap.com/service/cityList?'
 
 -     # 发送请求
 
 -     response = requests.get(base_url,headers=headers)
 
 -     # print(response.text)  
 
 -     
 
 -     #解析json数据
 
 -     json_data = response.json()
 
 -     # print(json_data) 得到的json数据放到在线json解析网站中,方便分析结构
 
 -     
 
 -     #获取adcode
 
 -     #热门城市
 
 -     city_adcode = []
 
 -     for data in json_data['data']['cityData']['hotCitys']:
 
 -         city_adcode.append((data['adcode'],data['name'],data['x'],data['y']))
 
 -     #其他城市
 
 -     #for data in json_data['data']['cityData']['otherCitys']:
 
 -         #city_adcode.append((data['adcode'],data['name'])) 
 
 -     return city_adcode
 
 -     print(city_adcode)
 
  
- def get_weather(adcode,city_name,X,Y):
 
 -     '''
 
 -     获取城市天气
 
 -     Query String Parameters
 
 -     adcode:500000
 
 -     '''
 
 -     #基础url
 
 -     base_url = 'https://www.amap.com/service/weather?adcode={}'.format(adcode)
 
 -     response = requests.get(base_url, headers=headers)  #发送请求,获取响应
 
 -     json_data = response.json()  #获取json数据
 
 -     #通过得到的json数据,在在线解析网站中解析后,分析其结构,找到要获取的在哪一个字典或者列表列表
 
 -     #分层的取出来即可
 
 -     if json_data['data']['result']=='true':
 
 -         weather = json_data['data']['data'][0]['forecast_data'][0]['weather_name']  #当前天气
 
 -         #最大温度
 
 -         max_temp = json_data['data']['data'][0]['forecast_data'][0]['max_temp']
 
 -         #最小温度
 
 -         min_temp = json_data['data']['data'][0]['forecast_data'][0]['min_temp']
 
 -         # print(weather, max_temp, min_temp)
 
 -         dic = {}
 
 -         dic['城市'] = city_name
 
 -         dic['adcode'] = adcode
 
 -         dic['X'] = X
 
 -         dic['Y'] = Y
 
 -         dic['天气'] = weather
 
 -         dic['温度'] = '{}/{}℃'.format(min_temp,max_temp)
 
 -         print(dic)
 
 -         
 
 -         
 
  
- def main():
 
 -     city_adcode = get_city()
 
 -     # print(city_adcode)
 
 -     #将每个城市的adcode传给get_weather
 
 -     #city_adcode有城市和adcode
 
 -     for i in city_adcode:
 
 -         adcode = i[0]
 
 -         city_name = i[1]
 
 -         X = i[2]
 
 -         Y = i[3]
 
 -         get_weather(adcode,city_name,X,Y)
 
  
 
-     
 
 - if __name__ == '__main__': 
 
 -     
 
 -     headers = {
 
 -         'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537',
 
 -         'X-Requested-With': 'XMLHttpRequest',
 
 -     }
 
 -     main()
 
  复制代码 
 本帖最后由 wp231957 于 2021-1-7 16:54 编辑 
是这些数据吗
 
import requests
 
base_url = 'https://www.amap.com/service/cityList?' 
response = requests.get(base_url) 
json_data = response.json()
 
zd=json_data["data"]["cityData"]["provinces"] 
for x in zd: 
   if len(zd[x]["cities"])>0 : 
      print(zd[x]["adcode"],zd[x]["name"],zd[x]["cities"][0]["name"]) 
   else:    
      print(zd[x]["adcode"],zd[x]["name"]) 
''' 
D:\wp>py wp\wp1.py 
110000 北京 
120000 天津 
130000 河北 石家庄 
140000 山西 太原 
150000 内蒙古 呼和浩特 
210000 辽宁 沈阳 
220000 吉林 长春 
230000 黑龙江 哈尔滨 
310000 上海 
320000 江苏 南京 
330000 浙江 杭州 
340000 安徽 合肥 
350000 福建 福州 
360000 江西 南昌 
370000 山东 济南 
410000 河南 郑州 
420000 湖北 武汉 
430000 湖南 长沙 
440000 广东 广州 
450000 广西 南宁 
460000 海南 海口 
500000 重庆 
510000 四川 成都 
520000 贵州 贵阳 
530000 云南 昆明 
540000 西藏 拉萨 
610000 陕西 西安 
620000 甘肃 兰州 
630000 青海 西宁 
640000 宁夏 银川 
650000 新疆 乌鲁木齐 
710000 台湾 
810000 香港 
820000 澳门 
'''   
 又简单的修正一下 
 
 
 |   
 
 
最佳答案
查看完整内容 
是这些数据吗
import requests
base_url = 'https://www.amap.com/service/cityList?'
response = requests.get(base_url)
json_data = response.json()
zd=json_data["data"]["cityData"]["provinces"]
for x in zd:
   if len(zd[x]["cities"])>0 :
      print(zd[x]["adcode"],zd[x]["name"],zd[x]["cities"][0]["name"])
   else:   
      print(zd[x]["adcode"],zd[x]["name"])
'''
D:\wp>py wp\wp1. ... 
 
 
 
 
 
 
 |