【工具】Python从临时邮箱获取验证码

安装好依赖库之后代码可直接运行, captcha = re.search(r'您的验证码为: \*(\w+)\*', response.json()['body']['html'])正则表达式部分改成自己的。

python 复制代码
import random
import requests
import re
from faker import Faker

domain = "https://api.mail.cx/api/v1" # 临时邮箱api
def generate_name():
    fake = Faker('en_US')
    while True:
        name = fake.name().replace(' ', '_')
        if len(name) <= 10:
            print(f"用户名: {name}")
            return name

def getAuth():
    url = domain + "/auth/authorize_token"
    headers = {
    'accept': 'application/json',
    'Authorization': 'Bearer undefined',
    }

    response = requests.post(url, headers=headers)

    return str(response.json())

def getMailAddress():
    root_mail = ["nqmo.com", "end.tw", "uuf.me", "yzm.de"]
    return generate_name() + '@' + random.choice(root_mail)
    

def getMailId(address, auth):
    url = domain + f"/mailbox/{address}"
    headers = {
        'accept': 'application/json',
        'Authorization': f'Bearer {auth}',
    }
    response = requests.get(url, headers=headers)
    body = response.json()
    return body[0]['id'] if len(body) and len(body[0]['id']) > 0 else None


def getCaptcha():
    # 获取token
    auth = getAuth()
    print(f"token: {auth}")
    # 获取邮箱地址
    address = getMailAddress()
    print(f"邮箱地址: {address}")
    # 等待获取验证码邮件
    id_ = None
    while id_ is None:
        id_ = getMailId(address, auth)
    # 获取验证码
    url = domain + f'/mailbox/{address}/{id_}'
    headers = {
        'accept': 'application/json',
        'Authorization': f'Bearer {auth}',
    }

    response = requests.get(url, headers=headers)
    # 正则匹配验证码,此处正则表达式匹配验证码改成自己的
    captcha = re.search(r'您的验证码为: \*(\w+)\*', response.json()['body']['html'])
    if captcha:
        print("验证码:", captcha.group(1))
    else:
        print("找不到验证码")
    return captcha.group(1)

if __name__ == '__main__':
   getCaptcha()
相关推荐
Hui Baby20 分钟前
Java SPI 与 Spring SPI
java·python·spring
我能坚持多久22 分钟前
D22—C语言预处理详解:从宏定义到条件编译
c语言·开发语言
小猪咪piggy25 分钟前
【Python】(3) 函数
开发语言·python
夜鸣笙笙29 分钟前
交换最小值和最大值
python
青岑CTF33 分钟前
攻防世界-Php_rce-胎教版wp
开发语言·安全·web安全·网络安全·php
2301_8223636034 分钟前
使用Pandas进行数据分析:从数据清洗到可视化
jvm·数据库·python
码界奇点1 小时前
基于Flask与OpenSSL的自签证书管理系统设计与实现
后端·python·flask·毕业设计·飞书·源代码管理
初次见面我叫泰隆1 小时前
Qt——4、Qt窗口
开发语言·qt·客户端开发
瑞雪兆丰年兮1 小时前
[从0开始学Java|第十一天]学生管理系统
java·开发语言
java1234_小锋1 小时前
分享一套优质的基于Python的房屋数据分析预测系统(scikit-learn机器学习+Flask)
python·数据分析·scikit-learn