因此,我開發了Python代碼,以文本消息的形式發送報價。它運行在一個簡單的腳本時運行正常。但是,一旦我將代碼放入一個函數並調用該函數,那麼它就無法工作。它發送一個空白的文本消息,而不是報價。Python代碼運行良好,直到放入函數
下面的代碼工作:
import smtplib
import urllib2
''' open webpage and grab randomly generated quote '''
response = urllib2.urlopen("http://inspirationalshit.com/quotes")
page = response.read()
split = page.split('<blockquote>')[1]
split = split.split('class="text-uppercase";>')[1]
quote = split.split('</p>')[0].strip()
'''email credentials (throwaway account) '''
username = "**********@gmail.com"
password = "*********"
phone = "********@vtext.com"
message = quote
''' format the email '''
msg = """From: %s
To: %s
Subject:
%s""" % (username, phone, message)
''' send the email '''
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(username, password)
server.sendmail(username, phone, msg)
server.quit()
這裏的非功能代碼
import urllib2
import smtplib
import schedule
import time
def job():
print "RAN"
''' open webpage and grab randomly generated quote '''
response = urllib2.urlopen("http://inspirationalshit.com/quotes")
page = response.read()
split = page.split('<blockquote>')[1]
split = split.split('class="text-uppercase";>')[1]
quote = split.split('</p>')[0].strip()
'''email credentials (throwaway account) '''
username = "*********@gmail.com"
password = "********"
phone = "********@vtext.com"
message = quote
''' format the email '''
msg = """From: %s
To: %s
Subject:
%s""" % (username, phone, message)
print msg
''' send the email '''
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(username, password)
server.sendmail(username, phone, msg)
server.quit()
job()
它看起來像你發送電子郵件,而不是短信。 – Chris
如果您嘗試在函數中打印(引用),是否打印預期的文本? – elsherbini
@elsherbini是的,它在兩種情況下打印相同的東西。 – Aweava