导入模块
''' 高德地图webAPI调用实例 author:poleung dataTime:2021-05-23 ''' # coding=utf-8 import json from urllib.request import urlopen
封装获取高德天气的函数
def getWeather(code): # 1.获取API url = "https://restapi.amap.com/v3/weather/weatherInfo?city={}&key=4d9a765939a2b76588a***c&output=JSON".format(code) res = urlopen(url) # 返回数据解码 data = json.loads(res.read()) # 数据格式输出 print(data['lives'][0]) print(data['lives'][0]['province']) # 调用函数 getWeather('110101')
结果输出到本地记事本
# 调用函数 data = getWeather('110101') # 输出文件 file = open('data.txt', 'w+', encoding='utf-8') file.write(json.dumps(data)) file.close()
lockdatav Done!