欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

运用python调用聚合数据接口查询电话号码归属地

程序员文章站 2022-05-19 11:58:55
...
#!/usr/bin/python
# -*- coding: utf-8 -*-
import json, urllib
import requests
import xlrd

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

def main():
    # 配置您申请的APPKey
    appkey = ""
    # 1.手机归属地查询
    request1(appkey, "GET")
# 手机归属地查询
def request1(appkey, m="GET"):

    data = xlrd.open_workbook(r'C:\Users\123456\Desktop\手机号/手机号.xlsx', 'utf-8')
    table = data.sheet_by_name('Sheet1')
    phone_list = list(table.col_values(0))
    file=open(r"C:\Users\123456\Desktop\手机号/号主城市.txt",'w')
    for i in range(65905,100000):
        try:
            phone = str(int(phone_list[i]))
            url = "http://apis.juhe.cn/mobile/get"
            params = {
                "phone": phone,  # 需要查询的手机号码或手机号码前7位
                "key": appkey,  # 应用APPKEY(应用详细页查询)
                "dtype": "",  # 返回数据的格式,xml或json,默认json
            }
            params = urllib.parse.urlencode(params)
            if m == "GET":
                f = requests.get("%s?%s" % (url, params)).content.decode('utf-8')
            else:
                f = requests.get(url, params).content.decode('utf-8')
            res = json.loads(f)
            if res:
                error_code = res["error_code"]
                if error_code == 0:
                    # 成功请求
                    file.write(phone + "," + res["result"]['province'] + "," + res["result"]['city'] + '\n')
                else:
                    print("%s:%s" % (res["error_code"], res["reason"]))
            else:
                print("request api error")
        except:pass



if __name__ == '__main__':
    main()