【工具】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()
相关推荐
子墨77726 分钟前
Flask之Hello world 详解
python
繁依Fanyi28 分钟前
旅游心动盲盒:开启个性化旅行新体验
java·服务器·python·算法·eclipse·tomcat·旅游
计算机编程-吉哥34 分钟前
计算机毕业设计 基于Python的个性化旅游线路推荐系统的设计与实现 Python+Django+Vue 前后端分离 附源码 讲解 文档
python·django·毕业设计·课程设计·毕业论文·计算机毕业设计选题·个性化旅游线路推荐系统
蜜桃小阿雯36 分钟前
JAVA开源项目 旅游管理系统 计算机毕业设计
java·开发语言·jvm·spring cloud·开源·intellij-idea·旅游
Benaso36 分钟前
Rust 快速入门(一)
开发语言·后端·rust
罔闻_spider38 分钟前
爬虫prc技术----小红书爬取解决xs
爬虫·python·算法·机器学习·自然语言处理·中文分词
python机器学习ML42 分钟前
机器学习K近邻算法——python详细代码解析(sklearn)(1)
python·机器学习·近邻算法·knn
wjs202444 分钟前
HTML5 新元素
开发语言
慕明翰1 小时前
Springboot集成JSP报 404
java·开发语言·spring boot
satan–01 小时前
R语言的基本语句及基本规则
开发语言·windows·r语言