赞
踩
- # coding=gbk
- import requests
- # 定义要使用的 API 地址和参数
- url = "http://api.kazhiguo.com/api/mobileempty/"
- api_key = "你的APIKEY"
-
- # 打开文件,将文件中的手机号逐行读取到列表中
- with open("phone1.txt") as file:
- phone_numbers = file.readlines()
-
- # 遍历手机号列表,发送请求并将结果写入不同的文件
- with open("empty.txt", "w") as empty_file, open("another.txt", "w") as another_file, open("error.txt", "w") as error_file:
- for phone in phone_numbers:
- # 清除手机号中的空白字符
- phone = phone.strip()
-
- # 如果手机号为空,则写入 empty.txt 文件中
- if not phone:
- empty_file.write(phone + "\n")
- else:
- # 更新请求参数中的手机号和 APIKEY
- params = {"apikey": api_key, "mobile": phone}
-
- # 发送请求
- response = requests.get(url, params=params)
-
- # 解析返回的 JSON 数据
- json_data = response.json()
-
- # 根据返回值将手机号写入不同的文件
- if json_data["code"] == 200:
- if json_data["result"]["status"] == 0:
- empty_file.write(phone + "\n")
- else:
- another_file.write(phone + "\n")
- else:
- error_file.write(phone +"/" +json_data["code"] + "/"+ json_data["error_code"])
- #another.txt为非空号
- #empty.txt为空号
- #返回码不是200则将错误代码输出到error.txt
逐行读取phone1.txt内的手机号
将空号输出到 empty.txt,将非空号输入到 not_empty.txt,如果返回值不是200将错误信息输入到 error.txt
第二版:
- # coding=gbk
- import requests
- # 定义要使用的 API 地址和参数
- url = "http://api.kazhiguo.com/api/mobileempty/"
- api_key = "你的APIKEY" #请手动更改
-
- # 打开文件,将文件中的手机号逐行读取到列表中
- with open("resultphone.txt") as file:
- phone_numbers = file.readlines()
-
- # 遍历手机号列表,发送请求并将结果写入不同的文件
- with open("empty.txt", "w") as empty_file, open("another.txt", "w") as another_file, open("error.txt", "w") as error_file, \
- open("空号.txt", "w") as konghao_file, open("实号.txt", "w") as shihao_file, open("停机.txt", "w") as tingji_file, open("库无.txt", "w") as kuwu_file, open("沉默号.txt", "w") as chenmohao_file, open("风险号.txt", "w") as fengxianhao_file, open("其他.txt", "w") as qita_file:
- for phone in phone_numbers:
- # 清除手机号中的空白字符
- phone = phone.strip()
-
- # 如果手机号为空,则写入 empty.txt 文件中
- if not phone:
- empty_file.write(phone + "\n")
- else:
- # 更新请求参数中的手机号和 APIKEY
- params = {"apikey": api_key, "mobile": phone}
-
- # 发送请求
- response = requests.get(url, params=params)
-
- # 解析返回的 JSON 数据
- json_data = response.json()
-
- # 根据返回值将手机号写入不同的文件
- if json_data["code"] == 200:
- if json_data["result"]["status"] == 0:
- empty_file.write(phone + "\n")
- else:
- another_file.write(phone + "\n")
- else:
- error_file.write(phone +"/" +json_data["code"] + "/"+ json_data["error_code"])
-
-
- if json_data["code"] == 200:
- if json_data["result"]["status"] == 0:
- konghao_file.write(phone + "\n")
- if json_data["result"]["status"] == 1:
- shihao_file.write(phone + "\n")
- if json_data["result"]["status"] == 2:
- tingji_file.write(phone + "\n")
- if json_data["result"]["status"] == 3:
- kuwu_file.write(phone + "\n")
- if json_data["result"]["status"] == 4:
- chenmohao_file.write(phone + "\n")
- if json_data["result"]["status"] == 5:
- fengxianhao_file.write(phone + "\n")
- else:
- qita_file.write(phone + "\n")
- else:
- pass
-
- #another.txt为非空号
- #empty.txt为空号
- #中文为号段分类
- #返回码不是200则将错误代码输出到error.txt
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。