python - 使用 IMAP 在 gmail 电子邮件正文中查找超链接
问题描述
该脚本的重点是在电子邮件中查找超链接并自动打开它们。我目前停留在搜索部分。
该脚本似乎无法从电子邮件正文中获取链接。超链接应该看起来像
https://something.com/verify/c4b7668ad547922226426896f
我的正则表达式有问题吗?
def process_mailbox(M):
rv, data = M.search(None, specific_email_addy)
if rv != 'OK':
print "No messages found!"
return
for num in data[0].split():
rv, data = M.fetch(num, '(RFC822)')
if rv != 'OK':
print "ERROR getting message", num
return
msg = email.message_from_string(data[0][1])
raw_email = data[0][1] # here's the body, which is raw headers and html and body of the whole email including headers and alternate payloads
msg = email.message_from_string(raw_email)
for part in msg.walk():
# each part is a either non-multipart, or another multipart message
# that contains further parts... Message is organized like a tree
if part.get_content_type() == 'text/html':
plain_text = part.get_payload()
link_pattern = re.compile('<a[^>]+href=\'(.*?)\'[^>]*>(.*)?</a>')
search = link_pattern.search(plain_text)
if search is not None:
print("Link found! -> " + search)
break
else:
print("No links were found.")
解决方案
推荐阅读
- watchman - 守望者的错误版本。找到的版本:2021.09.06.00 支持的版本:4.x
- python - Pyspark:计算两列中有多少行具有相同的值并删除重复项
- tensorflow - Tensorflow - 一批样本的矢量化映射函数
- wordpress - 自定义 Wordpress 订单问题
- c - 如何从C中的文本文件中删除最后一行
- java - 使用 Bouncycastle 在 Java 中进行格式保留加密 (FPE)
- amazon-web-services - AWS Lake Formation 与 AWS Step Functions
- webpack - Webpack 5 不转换节点模块 ESM 包
- typescript - TypeScript - 为函数使用泛型类型
- apache-flink - 将非结构化日志解析到flink中的数据集