「灰产?」Python生成电话号码并检测有效性
原创2022年10月14日大约 2 分钟...约 670 字
你好,我是悦创。
在之前的内容中,我们分享了办公自动化的 Excel 写入。
那现在这个部分呢,就是为了给你更好的练习,我来带你实现:自动生成电话号码并使用 api 验证电话号码有效性。
# -*- coding: utf-8 -*-
# @Time : 2022/10/13 19:27
# @Author : AI悦创
# @FileName: hw01.py
# @Software: PyCharm
# @Blog :https://bornforthis.cn/
import requests
import random, xlwt
# SET_PHONE = set()
def parse_loop(value):
template = "{haoduan}{weihao}"
# weihao = random.randrange(10000000, 99999999)
weihao = random.randint(11111111, 99999999)
# print(weihao)
# global phone_number
phone_number = template.format(haoduan=value, weihao=weihao)
# print(key, phone_number)
return phone_number
def generate_phone():
phone_dict = {
"中国联通": ['130', '131', '132', '145', '155', '156', '166', '175', '176', '185', '186', '196'],
"中国电信": ['133', '149', '153', '180', '181', '189', '173', '177', '190', '191', '193', '199'],
"中国移动": ['134', '135', '136', '137', '138', '139', '147', '150', '151', '152', '157', '158', '159', '172',
'178', '182', '183', '184', '187', '188', '195', '197', '198'],
}
key = random.choice(list(phone_dict.keys()))
value = random.choice(phone_dict[key])
phone_number = parse_loop(value)
# return key, phone_number
return phone_number
# 手机号码归属地查询 Python示例代码
def jude_phone(phone_number: str):
url = 'https://hcapi02.api.bdymkt.com/mobile'
params = {}
params['mobile'] = phone_number
headers = {
'Content-Type': 'application/json;charset=UTF-8',
'X-Bce-Signature': 'AppCode/38c548d4357c487185d818ed4da79a1a'
}
r = requests.request("GET", url, params=params, headers=headers)
# print(r.json())
return r.json()
def phone_number_exists(json):
if json["code"] == 200:
key = json["data"]["types"]
city = json["data"]["city"]
city_code = json["data"]["city_code"]
prov = json["data"]["prov"]
return [key, city, city_code, prov]
elif json["code"] == 400:
return False
def write_excel(content):
wb = xlwt.Workbook()
sheet = wb.add_sheet("sheet")
head_data = ["手机号", "类型", "城市", "城市代号", "省份"]
for index, head in enumerate(head_data):
sheet.write(0, index, head)
for row, detail_list in enumerate(content):
sheet.write(row+1, 0, detail_list[0])
sheet.write(row+1, 1, detail_list[1])
sheet.write(row+1, 2, detail_list[2])
sheet.write(row+1, 3, detail_list[3])
sheet.write(row+1, 4, detail_list[4])
wb.save("phone.xls")
def main():
r = []
for _ in range(1000):
result_lst = []
phone_number = generate_phone()
r_json = jude_phone(phone_number)
result_exists = phone_number_exists(r_json)
if result_exists:
result_lst.append(phone_number)
result_lst.extend(result_exists)
r.append(result_lst)
print("result_lst:>>>", result_lst)
else:
print("continue...")
continue
write_excel(r)
if __name__ == '__main__':
main()
# print(generate_phone())
欢迎关注我公众号:AI悦创,有更多更好玩的等你发现!
公众号:AI悦创【二维码】
AI悦创·编程一对一
AI悦创·推出辅导班啦,包括「Python 语言辅导班、C++ 辅导班、java 辅导班、算法/数据结构辅导班、少儿编程、pygame 游戏开发」,全部都是一对一教学:一对一辅导 + 一对一答疑 + 布置作业 + 项目实践等。当然,还有线下线上摄影课程、Photoshop、Premiere 一对一教学、QQ、微信在线,随时响应!微信:Jiabcdefh
C++ 信息奥赛题解,长期更新!长期招收一对一中小学信息奥赛集训,莆田、厦门地区有机会线下上门,其他地区线上。微信:Jiabcdefh
方法一:QQ
方法二:微信:Jiabcdefh
你认为这篇文章怎么样?
- 0
- 0
- 0
- 0
- 0
- 0