如何用Python获取163邮箱邮件信息?

摘要:此案例是是获取的一个亚马逊的验证码 import time from imaplib import IMAP4_SSL import imaplib,email,datetime from lxml import etree from da
此案例是是获取的一个亚马逊的验证码 import time from imaplib import IMAP4_SSL import imaplib,email,datetime from lxml import etree from dateutil.parser import parse def str_to_unicode(s, encoding=None): return str(s, encoding) if encoding else str(s) def get_xpath(xpath, content): # xpath 工具 out = [] tree = etree.HTML(content) results = tree.xpath(xpath) for result in results: if 'ElementStringResult' in str(type(result)) or 'ElementUnicodeResult' in str(type(result)): out.append(result) else: out.append(etree.tostring(result)) return out def send_mail(now_time): max_retries = 5 # 最大重试次数 retry_delay = 2 # 每次重试的间隔时间(秒) while True: status = 0 sender = 'XXX' # 邮箱 password = 'XXX' # 开启163邮箱的IMAP,获取授权码 # 尝试连接 IMAP 服务器 for attempt in range(max_retries): try: email_server = IMAP4_SSL(host='imap.163.com') break except Exception as e: if attempt < max_retries - 1: print(f"连接失败,正在重试... (尝试 {attempt + 1}/{max_retries})") time.sleep(retry_delay) else: print("连接失败,已达到最大重试次数。
阅读全文