可靠免费的天气接口

请访问我的github项目module-weather获取源程序,在这里只是长话短说


搜索了一下天气接口,发现很多都是收费接口,或者免费调用但是有次数限制。 因为项目上刚好用到天气模块,但是又不需要很具体的天气信息,所以萌生了开发一个基于中国天气数据的接口,中国天气的接口访问速度很快而且很稳定。
访问中国天气的接口有件麻烦事就是需要城市代码来查询。网上一搜城市代码,发现很多都是复制粘贴过来的。并没有json,xml等方便调用的格式。所以这个小项目的主要工作就是获取这些城市代码数据,所以说工作量不是很大。

直接上代码
1. 小爬虫

# !/usr/bin/env python
# -*- coding: utf-8 -*-

import json
import pickle
import urllib.request as request
from lxml import etree

url = 'http://cj.weather.com.cn/support/detail.aspx?id=51837fba1b35fe0f8411b6df'
save_dir = 'citycode.pkl'

try:
    r = request.Request(url)
    response = request.urlopen(r)
    html = response.read()
    #print(html)
    selector = etree.HTML(html)
    data = selector.xpath('//div[@class="entry-content"]//p/text()')[0:]
    code_dict = {}
    # 注意到北京分隔符有中文逗号也有西文逗号故独立出来处理
    code_dict['北京'] = data[0].strip().split(',')[0]
    for item in data[1:]:
        arr = item.strip().split(',')
        code_dict[arr[1]] = arr[0]
    print(code_dict)
    with open(save_dir, 'wb') as f:
        pickle.dump(code_dict, f, True)
    with open('./dict.json', 'w') as f:
        json.dump(code_dict, f, ensure_ascii=False)
except Exception as e:
print('faild> ', e)

访问接口

# !/usr/bin/env python
# -*- coding: utf-8 -*-

import datetime
import requests
from utils import *
from requests.packages.urllib3.exceptions import InsecureRequestWarning
#禁用安全警告
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

class Weather(object):
    def __init__(self, city, type='forecast'):
        self.type = type
        self.single_url = None
        self.multi_url = None
        code = get_code(city)
        if code != None:
            self.single_url = 'http://www.weather.com.cn/data/sk/{}.html'.format(code)
            self.multi_url = 'http://mobile.weather.com.cn/data/forecast/{}.html'.format(code)

    def process(self):
        if self.type == 'forecast':
            result = self.get_multi()
        else:
            result = self.get_single()
        return result

    def get_multi(self):
        result = {}
        if self.multi_url != None:
            r = requests.get(self.multi_url, verify=False)
            try:
                json = eval(str(r.content, 'utf-8'))
                result['type'] = 'forecast'
                result['city'] = json['c']['c3']
                result['results'] = []
                for i, item in enumerate(list(json['f']['f1'])):
                    now = datetime.datetime.now()
                    delta = datetime.timedelta(days=i)
                    n_days = now + delta
                    date = n_days.strftime('%Y-%m-%d')
                    temp = [item['fc'], item['fd']]
                    info = {
                        'date': date,
                        'temperature': {
                            'low': min(temp),
                            'high': max(temp)
                        },
                        'sunrise': item['fi'].split('|')[0],
                        'sunset': item['fi'].split('|')[1]
                    }
                    result['results'].append(info)
            except Exception as e:
                print('error ', e)
        return result

    def get_single(self):
        result = {}
        if self.single_url != None:
            r = requests.get(self.single_url, verify=False)
            try:
                json = eval(str(r.content, 'utf-8'))
                result['type'] = 'realtime'
                result['city'] = json['weatherinfo']['city']
                result['temperature'] = json['weatherinfo']['temp'] 
                result['wind'] = json['weatherinfo']['WD']
                result['rain'] = json['weatherinfo']['rain']
            except:
                return result
        return result

if __name__=='__main__':
    city = '深圳'
    print(city, '实时天气')
    print(Weather(city, 'realtime').process())

    print(city, '未来七天天气')
print(Weather(city).process())

实现效果

深圳实时天气
{'type': 'realtime', 'city': '深圳', 'temperature': '21', 'wind': '南风', 'rain': '0'}

深圳未来七天天气
{
  'type': 'forecast', 
   'city': '深圳', 
   'results': [
      {
        'date': '2017-08-31', 
        'temperature': {'low': '25', 'high': '32'}, 
        'sunrise': '06:18', 
        'sunset': '18:01'
      }, {
        'date': '2017-09-01', 
        'temperature': {'low': '25', 'high': '31'}, 
        'sunrise': '06:19', 
        'sunset': '18:00'
      }, {
        'date': '2017-09-02', 
        'temperature': {'low': '24', 'high': '27'}, 
        'sunrise': '06:19', 
        'sunset': '17:59'
      }, {
        'date': '2017-09-03', 
        'temperature': {'low': '21', 'high': '25'}, 
        'sunrise': '06:20', 
        'sunset': '17:58'
      }, {
        'date': '2017-09-04', 
        'temperature': {'low': '19', 'high': '23'}, 
        'sunrise': '06:20', 
        'sunset': '17:57'
      }, {
        'date': '2017-09-05', 
        'temperature': {'low': '20', 'high': '23'}, 
        'sunrise': '06:20', 
        'sunset': '17:56'
      }, {
        'date': '2017-09-06', 
        'temperature': {'low': '22', 'high': '25'}, 
        'sunrise': '06:21', 
        'sunset': '17:56'
      }
   ]
} 

附上城市代码下载地址

  1. csdn资源库 ,本想免积分的,可最低分要1分,所以土豪随意咯
  2. github文件 免积分,推荐
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值