Code worked last week - UnicoderEncodeError: 'ascii' codec can't encode characters - Python 3.X
Code worked last week - UnicoderEncodeError: 'ascii' codec can't encode characters - Python 3.X
所以,我上周写了这段代码,一切都很好 运行。我可以下载该文件,但现在它在尝试发送电子邮件时向我抛出一条错误消息。我环顾四周,似乎找不到任何专门解决此问题的内容。
UnicoderEncodeError:'ascii' 编解码器无法对位置 651-652 中的字符进行编码:序号不在 运行de(128)
中
感谢您提供的所有帮助。
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import time
import os
#eliminates the "save as" pop-up
download_dir = 'C:/Users/rkrouse/Desktop/Python/AZ'
fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList",2)
fp.set_preference("browser.download.manager.showWhenStarting", False)
fp.set_preference("browser.download.dir", download_dir)
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.ms-excel")
driver = webdriver.Firefox(firefox_profile=fp)
#navigate to website
driver.get('https://www.azroc.gov/roc/postinglist.html')
#click to download file
driver.find_element_by_partial_link_text("Dual").click()
time.sleep(5)
#sets up email delivery
EMAIL_SERVER = 'yada'
EMAIL_PORT = 25
EMAIL_FROM = 'yada'
RECIPIENT_LIST = ['yada']
msg = MIMEMultipart()
msg['From'] = EMAIL_FROM
msg['To'] = ", ".join(RECIPIENT_LIST)
msg['Subject'] = 'Arizona Dual Contractors List'
#email content
message = """<html>
<body>
Attached is the Arizona Contractors Spreadsheet.
<br><br>
Let me know if you have any questions
</body>
</html>
"""
msg.attach(MIMEText(message, 'html'))
#attaches file to email
filename = r'C:\Users\rkrouse\Downloads\active_contractor_dual.xls'
attachment = open(filename, 'rb')
part = MIMEBase('application','octet-stream')
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition','attachment; filename= '+filename)
msg.attach(part)
#sends email
smtpserver = smtplib.SMTP(EMAIL_SERVER, EMAIL_PORT)
smtpserver.sendmail(EMAIL_FROM, RECIPIENT_LIST, msg.as_string())
smtpserver.quit()
#closes file and deletes from directory
attachment.close()
os.remove(filename)
encoders.encode_base64(part)
需要在设置payload和header的时候加上。有问题的代码已更正以反映解决方案。
所以,我上周写了这段代码,一切都很好 运行。我可以下载该文件,但现在它在尝试发送电子邮件时向我抛出一条错误消息。我环顾四周,似乎找不到任何专门解决此问题的内容。
UnicoderEncodeError:'ascii' 编解码器无法对位置 651-652 中的字符进行编码:序号不在 运行de(128)
中感谢您提供的所有帮助。
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import time
import os
#eliminates the "save as" pop-up
download_dir = 'C:/Users/rkrouse/Desktop/Python/AZ'
fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList",2)
fp.set_preference("browser.download.manager.showWhenStarting", False)
fp.set_preference("browser.download.dir", download_dir)
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.ms-excel")
driver = webdriver.Firefox(firefox_profile=fp)
#navigate to website
driver.get('https://www.azroc.gov/roc/postinglist.html')
#click to download file
driver.find_element_by_partial_link_text("Dual").click()
time.sleep(5)
#sets up email delivery
EMAIL_SERVER = 'yada'
EMAIL_PORT = 25
EMAIL_FROM = 'yada'
RECIPIENT_LIST = ['yada']
msg = MIMEMultipart()
msg['From'] = EMAIL_FROM
msg['To'] = ", ".join(RECIPIENT_LIST)
msg['Subject'] = 'Arizona Dual Contractors List'
#email content
message = """<html>
<body>
Attached is the Arizona Contractors Spreadsheet.
<br><br>
Let me know if you have any questions
</body>
</html>
"""
msg.attach(MIMEText(message, 'html'))
#attaches file to email
filename = r'C:\Users\rkrouse\Downloads\active_contractor_dual.xls'
attachment = open(filename, 'rb')
part = MIMEBase('application','octet-stream')
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition','attachment; filename= '+filename)
msg.attach(part)
#sends email
smtpserver = smtplib.SMTP(EMAIL_SERVER, EMAIL_PORT)
smtpserver.sendmail(EMAIL_FROM, RECIPIENT_LIST, msg.as_string())
smtpserver.quit()
#closes file and deletes from directory
attachment.close()
os.remove(filename)
encoders.encode_base64(part)
需要在设置payload和header的时候加上。有问题的代码已更正以反映解决方案。