一、开通接口
免费手机号码(段)归属地查询服务使用聚合数据提供的免费接口,每天可以100次免费调用。可以通过 https://www.juhe.cn/docs/api/id/11 注册及开通。
二、请求接口
#!/usr/bin/python
# -*- coding: utf-8 -*-
import urllib, urllib2, sys, json
reload(sys)
sys.setdefaultencoding('utf-8')
url = 'http://apis.juhe.cn/mobile/get'
params = {
"phone": "13429667914", # 查询的手机号码或手机号码前7位
"key": "******", # 您申请的API接口请求Key
}
querys = urllib.urlencode(params)
request = urllib2.Request(url, data=querys)
response = urllib2.urlopen(request)
content = response.read()
if (content):
try:
result = json.loads(content)
error_code = result['error_code']
if (error_code == 0):
province = result['result']['province']
city = result['result']['city']
areacode = result['result']['areacode']
zip = result['result']['zip']
company = result['result']['company']
print("省份:%s\n城市:%s\n区号:%s\n邮编:%s\n运营商:%s" % (province, city, areacode, zip, company))
else:
print("请求失败:%s %s" % (result['error_code'], result['reason']))
except Exception as e:
print("解析结果异常:%s" % e)
else:
# 可能网络异常等问题,无法获取返回内容,请求异常
print("请求异常")