百度提供的获取天气的api url :
http://api.map.baidu.com/telematics/v3/weather?location= xian &output=json&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&callback=?'
获取当前日期, 及当日温馨提示, 和未来几天的温度;
并通过微信发送给指定好友(itchat在博客里已经提过这里不再写给指定微信好友发送消息).
获取天气的api:
python-json练习

from urllib.request import urlopen
import json
url = 'http://api.map.baidu.com/telematics/v3/weather?location=%20xian%20&output=json&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&callback=?%27'
urlObj = urlopen(url)

pageContent = urlObj.read().decode('utf-8')
dict_data=json.loads(pageContent)

print("""
今日日期:%s
温馨提醒:%s
未来1天的温度:%s
未来2天的温度:%s
""" %(dict_data['date'],dict(*dict_data['results'])['index'][0]['des'],dict(*dict_data['results'])['weather_data'][1]['temperature'],dict(*dict_data['results'])['weather_data'][2]['temperature']))
# 这里需要将字典里嵌套的列表解包:* ,解包转换为字典再进行key-value操作。

python换行自动对齐,有助于看这个大字典的嵌套里的key值
python-json练习
python-json练习

相关文章:

  • 2021-12-02
猜你喜欢
  • 2022-03-03
  • 2022-12-23
  • 2022-01-04
  • 2021-09-10
  • 2021-05-02
相关资源
相似解决方案