2 回答

TA貢獻(xiàn)1155條經(jīng)驗(yàn) 獲得超0個贊
正如您的錯誤所表明的那樣,您需要將您的return 內(nèi)部函數(shù)
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貢獻(xiàn)1807條經(jīng)驗(yàn) 獲得超9個贊
保留字return只能在如下函數(shù)中使用:
def hello(name):
return "hello " + name
如果你不打算在一個函數(shù)內(nèi)工作(你現(xiàn)在不是)嘗試做這樣的事情:
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)
發(fā)生的事情是您現(xiàn)在將span.text對象添加到名為emails. 然后您可以使用該列表供以后使用。
添加回答
舉報(bào)