【工具】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()
相关推荐
努力成为AK大王12 小时前
Java并发线程核心知识(一)
java·开发语言·面试
smj2302_7968265212 小时前
解决leetcode第3934题最短唯一子数组
数据结构·python·算法·leetcode
t-think13 小时前
深入理解指针(2)
c语言·开发语言
iiiiyu13 小时前
面向对象和集合编程题
java·开发语言·前端·数据结构·算法·编程语言
geovindu13 小时前
go: Read-Write Lock Pattern
开发语言·后端·设计模式·golang·读写锁模式
WL_Aurora13 小时前
Python 算法基础篇之查找算法(二):斐波那契查找、分块查找与哈希查找
python·哈希算法·查找
牙牙要健康13 小时前
Windows 下为 VSCode 配置 Anaconda:从零安装 Python 环境到完整配置教程
windows·vscode·python
Full Stack Developme13 小时前
JDK 发展历史
java·开发语言
财经资讯数据_灵砚智能13 小时前
基于全球经济类多源新闻的NLP情感分析与数据可视化(夜间-次晨)2026年5月17日
大数据·人工智能·python·信息可视化·自然语言处理
databook13 小时前
切线的魔法:用 SymPy 和 Manim 轻松搞定导数动画
python·数学·动效