Python:基于聚合接口的手机号归属地查询

#!/usr/bin/python
# -*- coding: utf-8 -*-
import json, urllib
from urllib import urlencode


# ----------------------------------
# 手机号码归属地调用示例代码 - 聚合数据
# 在线接口文档:https://www.juhe.cn/docs/api/id/11
# ----------------------------------

def main():
    # 配置您申请的APPKey
    # appkey = "*****************************"
    appkey = "0ea8e44e4612fb794c29f4979de48ef7"

    # 1.手机归属地查询
    request1(appkey, "GET")


# 手机归属地查询
def request1(appkey, m="GET"):
    url = "http://apis.juhe.cn/mobile/get"
    params = {
        "phone": "1735420",  # 需要查询的手机号码或手机号码前7位
        "key": appkey,  # 应用APPKEY(应用详细页查询)
        "dtype": "",  # 返回数据的格式,xml或json,默认json

    }
    params = urlencode(params)
    if m == "GET":
        f = urllib.urlopen("%s?%s" % (url, params))
    else:
        f = urllib.urlopen(url, params)

    content = f.read()
    res = json.loads(content)
    #res=json.dumps(json.loads(content),ensure_ascii=False)
    if res:
        error_code = res["error_code"]
        if error_code == 0:
            # 成功请求
           print json.dumps(res["result"], ensure_ascii=False) #字符转中文
            #print res["result"]
        else:
            print "%s:%s" % (res["error_code"], res["reason"])
    else:
        print "request api error"


if __name__ == '__main__':
    main()