【工具】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()
相关推荐
red润4 分钟前
python win32COM 对象介绍调用Word、WPS 与应用生态
python
旦莫6 分钟前
Python测试开发工具库:测试环境变量统一配置与加载工具
python·测试开发·自动化·ai测试
Echo缘7 分钟前
关于在.cpp文件中包含c的头文件,编译报错问题
c语言·开发语言
Hello.Reader7 分钟前
Flink Avro Format Java / PyFlink 读写、Schema 细节与坑点总结
java·python·flink
人道领域11 分钟前
【零基础学java】(反射)
java·开发语言
ghie909019 分钟前
GPS抗干扰算法MATLAB实现
开发语言·算法·matlab
ytttr87327 分钟前
基于MATLAB解决车辆路径问题(VRP)
开发语言·matlab
我是海飞27 分钟前
杰理 AC792N WebSocket 客户端例程使用测试教程
c语言·python·单片机·websocket·网络协议·嵌入式·杰理
沛沛老爹28 分钟前
Web开发者突围AI战场:Agent Skills元工具性能优化实战指南——像优化Spring Boot一样提升AI吞吐量
java·开发语言·人工智能·spring boot·性能优化·架构·企业开发