gpt4 book ai didi

python - 代码上周有效 - UnicodeEncodeError : 'ascii' codec can't encode characters - Python 3. X

转载 作者:太空宇宙 更新时间:2023-11-03 21:47:36 25 4
gpt4 key购买 nike

所以,我上周编写了这段代码,一切都运行得很好。我可以下载该文件,但现在在尝试发送电子邮件时向我抛出一条错误消息。我环顾四周,似乎找不到任何专门解决此问题的内容。

UnicoderEncodeError:“ascii”编解码器无法对位置 651-652 中的字符进行编码:序数不在 rande(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时需要添加。有问题的代码已被更正以反射(reflect)解决方案。

关于python - 代码上周有效 - UnicodeEncodeError : 'ascii' codec can't encode characters - Python 3. X,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52373113/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com