【问题标题】:Ascii Code Error when using SMTP Gmail Python [duplicate]使用 SMTP Gmail Python 时出现 Ascii 代码错误 [重复]
【发布时间】:2022-01-11 06:10:41
【问题描述】:

每当列表产品的最低价格发生变化时,我都在网上抓取亚马逊并尝试向收件人发送电子邮件。我的代码如下。 j 是 {title:price} 的字典。 title_rating 是 {title:rating} 等的字典。

def create_message():
    message = f""
    for title in j:
        #print(title)
        cr_price = j[title]
        cr_rating = title_rating[title]
        cr_reviews = title_num_reviews[title]
        cr_avail = title_avail[title]
        str1 = f"Product Title = {title}\n"
        str2 = f"{str1}Product Price = {cr_price}\n"
        str3 = f"{str2}Product Rating = {cr_rating}\n"
        str4 = f"{str3}Number of Product Reviews = {cr_reviews}\n"
        str5 = f"{str4}Availability = {cr_avail}\n\n"
        message += str5 #str1 + str2 + str3 + str4 + str5
    print("message sent")

    return message 
def send_email(message, sender_email, sender_password, receiver_email):
    s = smtplib.SMTP('smtp.gmail.com', 587)
    s.starttls()
    s.login(sender_email, sender_password)
    s.sendmail(sender_email, receiver_email, message)
    s.quit()

我收到以下消息。

【问题讨论】:

  • @0x263A 是的,确实如此。不过我选择了不同的路线。

标签: python smtp ascii smtplib


【解决方案1】:

我发现 \u2013 是一个连字符并决定告诉消息忽略它

message = message.replace("\u2013", " ")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-18
    • 1970-01-01
    • 2012-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-06
    • 1970-01-01
    相关资源
    最近更新 更多