请访问我的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'
}
]
}
附上城市代码下载地址