最近在学习用Python开发微信公共账号,正在做的是一个基本的生活服务类的平台。为了实现查询天气、翻译、物流等生活信息,需要使用其他服务商提供的接口。而云聚就是这样的一个服务平台。它提供的接口调用方法简单,易用,对于我足够用了。
1.实时天气
调用格式:http://api.36wu.com/Weather/GetWeather?city=%E5%8C%97%E4%BA%AC
返回数据的格式有json和xml两种,个人觉得json格式用python处理起来比较简单。
# -*- coding:utf-8 -*-
import json
import urllib
import urllib2
city = urllib.quote('北京')
baseurl = r'http://api.36wu.com/Weather/GetWeather?city='
url = baseurl + city
respr = json.loads(urllib2.urlopen(url).read())
trans = u'%s,%s,%s,%s~%s,%s' % \
(respr['data']['city'],''.join(respr['data']['dateTime'])\
,''.join(respr['data']['temp']),''.join(respr['data']['minTemp'])\
,''.join(respr['data']['maxTemp']),''.join(respr['data']['weather']))
print trans
2.翻译服务
调用格式:http://api.36wu.com/Translate/GetTranslate?q=%E6%88%91%E6%98%AF%E4%B8%AD%E5%9B%BD%E4%BA%BA%EF%BC%8C%E6%88%91%E7%88%B1%E6%88%91%E7%9A%84%E5%9B%BD%E5%AE%B6&format=auto
# -*- coding:utf-8 -*-
import json
import urllib
import urllib2
sentence = urllib.quote('我是一个程序员')
baseurl = r'http://api.36wu.com/Translate/GetTranslate?q='
url = baseurl + sentence + '&format=auto'
respr = json.loads(urllib2.urlopen(url).read())
trans = '%s,%s'%(respr['data'][0]['src'],''.join(respr['data'][0]['dst']))
print trans
3.查询手机归属地
调用格式:http://api.36wu.com/Mobile/GetMobileOwnership?mobile=手机号码
# -*- coding:utf-8 -*-
import json
import urllib
import urllib2
number = raw_input("Please enter a phone number:")
baseurl = r'http://api.36wu.com/Mobile/GetMobileOwnership?mobile='
url = baseurl + number
reprs = json.loads(urllib2.urlopen(url).read())
trans = u'%s,%s,%s' % (reprs['data']['province'],''.join(reprs['data']['city']),''.join(reprs['data']['corp']))
print trans
--------------------未完待续--------------------