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. 然后您可以使用該列表供以后使用。
添加回答
舉報