第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

返回語句不返回電子郵件的值

返回語句不返回電子郵件的值

慕的地8271018 2021-11-30 18:28:18
我正在嘗試編寫一些一旦運(yùn)行就會返回電子郵件正文的內(nèi)容。到目前為止我所擁有的是:from exchangelib import Credentials, Accountimport urllib3from bs4 import BeautifulSoupcredentials = 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),它會完美運(yùn)行并打印電子郵件的正文。但是,當(dāng)替換為 時return,它會引發(fā)錯誤讀數(shù)SyntaxError: 'return' outside function。我一直在研究這個問題,我似乎無法弄清楚它為什么會拋出這個問題。我是 Python 新手,可以使用一些幫助。我能做些什么來解決這個問題?
查看完整描述

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



查看完整回答
反對 回復(fù) 2021-11-30
?
函數(shù)式編程

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. 然后您可以使用該列表供以后使用。


查看完整回答
反對 回復(fù) 2021-11-30
  • 2 回答
  • 0 關(guān)注
  • 178 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號