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

时间:2019-11-17 17:31:34

标签: python python-3.x web-scraping unicode

我正在尝试建立一个小的脚本,该脚本检查亚马逊产品的价格,如果价格降低了一定值,则会向我发送电子邮件,但我却收到错误消息,我既不了解错误也不知道如何处理我可以修复它吗,因为它似乎在味精内部:

import requests
from bs4 import BeautifulSoup
import smtplib

url = 'https://www.amazon.de/dp/B07XRR92LP?ref_=ast_sto_dp&th=1'

headers = {'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Safari/605.1.15'}

def check_price():
    page = requests.get(url, headers = headers)
    soup = BeautifulSoup(page.content, 'html.parser')


    price = soup.find(id = 'priceblock_ourprice').get_text()
    converted_price = float(price[0:5])
    if converted_price < 1000:
        send_mail()

    print(converted_price)

    if converted_price < 1000:
        send_mail()

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

    server.login('----','----')

    subject = 'COMPRA'
    body= 'El iPhone 11 pro ha bajado de 1000€. https://www.amazon.de/dp/B07XRR92LP?ref_=ast_sto_dp&th=1'

    msg = f'Subject: {subject}\n\n{body}'
    server.sendmail(
    '----',
    '----',
    msg)

    print('Hey, Email has been sent')

    server.quit()


check_price()

它返回错误


----> 1 check_price()

<ipython-input-46-6cb2f9cb11a6> in check_price()
      7     converted_price = float(price[0:5])
      8     if converted_price < 1000:
----> 9         send_mail()
     10 
     11     print(converted_price)

<ipython-input-51-3bd5401c5d79> in send_mail()
     14     '----',
     15     '----',
---> 16     msg)
     17 
     18     print('Hey, Email has been sent')

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py in sendmail(self, from_addr, to_addrs, msg, mail_options, rcpt_options)
    853         esmtp_opts = []
    854         if isinstance(msg, str):
--> 855             msg = _fix_eols(msg).encode('ascii')
    856         if self.does_esmtp:
    857             if self.has_extn('size'):

UnicodeEncodeError: 'ascii' codec can't encode character '\u20ac' in position 53: ordinal not in range(128)

谢谢。

0 个答案:

没有答案