我正在尝试编写一些一旦运行就会返回电子邮件正文的内容。到目前为止我所拥有的是:
from exchangelib import Credentials, Account
import urllib3
from bs4 import BeautifulSoup
credentials = Credentials('fake@email', 'password')
account = Account('fake@email', credentials=credentials, autodiscover=True)
for item in account.inbox.all().order_by('-datetime_received')[:1]:
html = item.unique_body
soup = BeautifulSoup(html, "html.parser")
for span in soup.find_all('font'):
return span.text
我的问题是最后一行阅读return span.text。如果我用 替换这一行print(span.text),它会完美运行并打印电子邮件的正文。但是,当替换为 时return,它会引发错误读数SyntaxError: 'return' outside function。我一直在研究这个问题,我似乎无法弄清楚它为什么会抛出这个问题。我是 Python 新手,可以使用一些帮助。我能做些什么来解决这个问题?
白衣非少年
函数式编程
相关分类