2 回答

TA贡献1155条经验 获得超0个赞
正如您的错误所表明的那样,您需要将您的return 内部函数
from exchangelib import Credentials, Account
import urllib3
from bs4 import BeautifulSoup
credentials = Credentials('fake@email', 'password')
account = Account('fake@email', credentials=credentials, autodiscover=True)
def get_email(span): # a function that can return values
return span.text
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'):
email_result = get_email(span) # call function and save returned value in a variable

TA贡献1807条经验 获得超9个赞
保留字return只能在如下函数中使用:
def hello(name):
return "hello " + name
如果你不打算在一个函数内工作(你现在不是)尝试做这样的事情:
emails = []
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'):
emails.append(span.text)
发生的事情是您现在将span.text对象添加到名为emails. 然后您可以使用该列表供以后使用。
添加回答
举报