【工具】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()
相关推荐
路多辛9 分钟前
全能型 Go Agent 框架 covonaut v1.0.8 发布:新增行内补全与多后端可观测性
开发语言·golang·agent
大模型丫丫11 分钟前
FastAPI 入门指南:从零开始构建高性能 Python API
开发语言·python·fastapi
叫我:松哥19 分钟前
基于flask图书数据管理系统,技术栈flask+MySQL+boostrap
python·mysql·flask
wangchen_022 分钟前
PyTorch
人工智能·pytorch·python
wuyk55522 分钟前
《WiFi 嵌入式物联网开发全套实战》| 第01章 WiFi整体架构详解:BSS/ESS/AP/STA模式彻底区分
开发语言·stm32·单片机·嵌入式硬件·mcu·物联网·51单片机
wuyk55528 分钟前
第六章:CAN 调试工具、抓包分析、全套故障排查 + CAN-FD 进阶拓展
开发语言·stm32·单片机
卷无止境1 小时前
Python的contextlib与 FastAPI 中的上下文管理
后端·python·fastapi
测试19981 小时前
接口自动化测试的全面解析与实战指南
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·接口测试
whcyhhh1 小时前
头歌实践教学平台:数据科学与大数据技术导论(九上)
大数据·python
2501_915106321 小时前
Swift 开发环境搭建指南:三条路径按目标对号入座
开发语言·vscode·ios·objective-c·个人开发·swift·敏捷流程