python结合API实现即时天气信息

过去的习惯,决定今天的你,所以,过去的懒惰,决定你今天的一败涂地。人来到这世界后,命运注定了他必须要拼搏,奋斗,坚持,勇敢地走下去,走出属于自己的道路,没有人能不劳而获。

python结合API实现即时天气信息

import urllib.request
import urllib.parse
import json
 
"""
 利用“最美天气”抓取即时天气情况
 http://www.zuimeitianqi.com/
 
"""
class ZuiMei():
 def __init__(self):
  self.url = 'http://www.zuimeitianqi.com/zuimei/queryWeather'
  self.headers = {}
  self.headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36'
  # 部分城市的id信息
  self.cities = {}
  self.cities['成都'] ='01012703'
  self.cities['杭州'] = '01013401'
  self.cities['深圳'] = '01010715'
  self.cities['广州'] = '01010704'
  self.cities['上海'] = '01012601'
  self.cities['北京'] = '01010101'
  # Form Data
  self.data = {}
  self.city = '北京'
  
 def query(self,city='北京'):
  if city not in self.cities:
   print('暂时不支持当前城市')
   return
  self.city = city
  data = urllib.parse.urlencode({'cityCode':self.cities[self.city]}).encode('utf-8')
  req = urllib.request.Request(self.url,data,self.headers)
  response = urllib.request.urlopen(req)
 
  html = response.read().decode('utf-8')
  # 解析json数据并打印结果
  self.json_parse(html)
 
 def json_parse(self,html):
  target = json.loads(html)
  high_temp = target['data'][0]['actual']['high']
  low_temp = target['data'][0]['actual']['low']
  current_temp = target['data'][0]['actual']['tmp']
  today_wea = target['data'][0]['actual']['wea']
  air_desc = target['data'][0]['actual']['desc']
  # 上海 6~-2°C 现在温度 1°C 湿度:53 空气质量不好,注意防霾。 
  print('%s: %s~%s°C 现在温度 %s°C 湿度:%s %s'%(self.city,high_temp,low_temp,current_temp,today_wea,air_desc))

if __name__ == '__main__':
 zuimei = ZuiMei()
 zuimei.query('广州')

效果演示:

到此这篇关于python结合API实现即时天气信息就介绍到这了。自己喜欢的东西就不要问别人好不好看,自己决定做的事就不要问别人同不同意。你的生活,不会因为别人的话而变好。但,会因为你的努力而有更多可能性!更多相关python结合API实现即时天气信息内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!

您可能有感兴趣的文章
Python自动化运维-使用Python脚本监控华为AR路由器关键路由变化

Python自动化运维-netmiko模块设备自动发现

Python自动化运维—netmiko模块连接并配置华为交换机

Python自动化运维-利用Python-netmiko模块备份设备配置

Python自动化运维-Paramiko模块和堡垒机实战