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 獲取郵箱驗證碼
所需要的庫自己安裝,郵箱參數(shù)自己寫。默認獲取的是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位數(shù)字,這里根據(jù)需要改成自己的正則表達式
code.append(str(re.findall(r"\d{6}", str(s))[0]))
print(code[len(code) - 1])到此這篇關(guān)于Python如何從臨時郵箱獲取驗證碼的文章就介紹到這了,更多相關(guān)Python獲取驗證碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python機器學(xué)習(xí)Github已達8.9Kstars模型解釋器LIME
這篇文章主要為大家介紹了Github已達8.9Kstars的最佳模型解釋器LIME的使用示例及功能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-11-11
利用python實現(xiàn)后端寫網(wǎng)頁(flask框架)
這篇文章主要給大家介紹了關(guān)于如何利用python實現(xiàn)后端寫網(wǎng)頁(flask框架)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
Python實例方法與類方法和靜態(tài)方法介紹與區(qū)別分析
在 Python 中,實例方法(instance method),類方法(class method)與靜態(tài)方法(static method)經(jīng)常容易混淆。本文通過代碼例子來說明它們的區(qū)別2022-10-10
Python 實現(xiàn)數(shù)據(jù)結(jié)構(gòu)中的的棧隊列
這篇文章主要介紹了Python 實現(xiàn)數(shù)據(jù)結(jié)構(gòu)中的的棧,隊列,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-05-05

