UnicodeEncodeError:'ascii'编解码器无法在位置248处编码字符'\ u20b9':序数不在范围内(128)

时间:2019-12-29 19:49:52

标签: python python-3.x beautifulsoup python-requests

我尝试制作一个网络抓取工具来跟踪亚马逊的价格,并在价格发生任何变化或波动时向我发送电子邮件警报,但这是我遇到的错误,对此我还很陌生。

详细错误:

    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()

1 个答案:

答案 0 :(得分:0)

这是由于卢比无法使用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'。这就是我们可以对INR货币符号进行编码的原因。