python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Python获取验证码

Python从临时邮箱获取验证码的操作代码

作者:东西山海关_cy

这篇文章主要介绍了Python从临时邮箱获取验证码的操作代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

Python如何从临时邮箱获取验证码

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

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()

python 获取邮箱验证码

所需要的库自己安装,邮箱参数自己写。默认获取的是6位验证码

from imbox import Imbox
import time
from datetime import datetime
import re
with Imbox('outlook.office365.com',
        username='邮箱',
        password='密码',
        ssl=True,
        ssl_context=None,
        starttls=False) as imbox:
    all_inbox_messages = imbox.messages(unread=True)
    #all_inbox_messages = imbox.messages()
    code = []
    for uid, message in all_inbox_messages:
        msg = message.body['plain'][0].encode('utf-8')
        s = re.findall(r"code: \d{6}", str(msg))##有可能多个6位数字,这里根据需要改成自己的正则表达式
        code.append(str(re.findall(r"\d{6}", str(s))[0]))
    print(code[len(code) - 1])

到此这篇关于Python如何从临时邮箱获取验证码的文章就介绍到这了,更多相关Python获取验证码内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文