常用的生活服务查询接口--使用python

最近在学习用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


--------------------未完待续--------------------



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值