UnicodeEncodeError:“ascii”编解码器无法对位置248中的字符“\u20b9”进行编码:序号不在范围内(128)

2024-09-30 13:34:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着做一个网页刮板跟踪亚马逊的价格,并给我发送电子邮件提醒,每当有一些变化或波动的价格,但这是我得到的错误,我对此相当陌生。你知道吗

详细错误:

    Traceback (most recent call last):
  File "/Users/vaibhav/Desktop/labai/scraper.py", line 53, in <module>
    check_price()
  File "/Users/vaibhav/Desktop/labai/scraper.py", line 20, in check_price
    send_mail()
  File "/Users/vaibhav/Desktop/labai/scraper.py", line 45, in send_mail
    msg
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py", line 855, in sendmail
    msg = _fix_eols(msg).encode('ascii')
UnicodeEncodeError: 'ascii' codec can't encode character '\u20b9' in position 248: ordinal not in range(128)

我写的Python代码

    import requests
    from bs4 import BeautifulSoup
    import smtplib


    URL = 'https://www.amazon.in/Nokia-Designer-Protective-Printed-Doesnt/dp/B078MFZS9V/ref=bbp_bb_a77114_st_KIqx_w_1?psc=1&smid=A2V1Y4Y0T37MVF'
    headers = {example user agent}


    def check_price():
        page = requests.get(URL,headers = headers)

    soup = BeautifulSoup(page.content,'html.parser')

    title = soup.find(id="productTitle").get_text()
    price = soup.find(id="priceblock_ourprice").get_text()
    converted_price = float(price[2:5])

    if(converted_price<400):
        send_mail()

    print(title.strip())
    print(converted_price)


    if(converted_price>300):
        send_mail()

def send_mail():
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.ehlo()
    server.starttls()
    server.ehlo()

    server.login(''example@exampleemail'','examplepass')

    subject = 'Price fell down'
    body =  'Check the amazon link  https://www.amazon.in/dp/B07XVKG5XV?aaxitk=Afmq.hE.Dq.i9ttZqy2U9g&pd_rd_i=B07XVKG5XV&pf_rd_p=2e3653de-1bdf-402d-9355-0b76590c54fe&hsa_cr_id=4398426540602&sb-ci-n=price&sb-ci-v=64%2C899.00&sb-ci-m=₹'

    msg = f"Subject = {subject}\n\n{body}"

    server.sendmail(
        'example@exampleemail',
        'example@exampleemail',
        msg
    )

    print('HEY MAIL HAS BEEN SENT')

    server.quit()


check_price()

Tags: inpysendserverexamplechecklinemail
1条回答
网友
1楼 · 发布于 2024-09-30 13:34:58

这是因为卢比的货币符号₹无法用ASCII编码。相反,您可能希望为smtplib启用UTF-8(或其他一些unicode编码)。最简单的方法是使用email (link is to examples)模块。你知道吗

import smtplib
from email.mime.text import MIMEText

text_type = 'plain' # or 'html'
text = 'Your message body'
msg = MIMEText(text, text_type, 'utf-8')
msg['Subject'] = 'Test Subject'
msg['From'] = gmail_user
msg['To'] = 'user1@x.com,user2@y.com'
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.login(gmail_user, gmail_password)
server.send_message(msg)
# or server.sendmail(msg['From'], msg['To'], msg.as_string())
server.quit()

this答案复制的代码。你知道吗

注意,在MIMEText中我们使用'utf-8'。这使我们能够对印度卢比货币符号进行编码。你知道吗

相关问题 更多 >

    热门问题