zoukankan      html  css  js  c++  java
  • “最美天气”Python抓取天气

    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)
            actual = target['data'][0]['actual']
            high_temp = actual['high']
            low_temp = actual['low']
            current_temp = actual['tmp']
            today_wea = actual['wea']
            air_desc = 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('广州')


  • 相关阅读:
    GreenPlum 锁表以及解除锁定
    Postgresql 解决锁表
    Greenplum 查看连接与锁信息数据字典
    Greenplum 常用数据字典
    Linux 内核参数说明
    Greenplum 如何直连segment节点
    GreenPlum 数据备份与恢复
    unity 获取DontDestroyOnLoad的游戏对象
    scheduleOnce时出错,CCScheduler#scheduleSelector. Selector already scheduled. Updating interval from: 0 to 0"
    正比适配,留黑边
  • 原文地址:https://www.cnblogs.com/lkpp/p/7400044.html
Copyright © 2011-2022 走看看