Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python UnicodeEncodeError:&x27;ascii';编解码器可以';t编码字符'\u20b9';位置248:序号不在范围内(128)_Python_Python 3.x_Beautifulsoup_Python Requests - Fatal编程技术网

Python UnicodeEncodeError:&x27;ascii';编解码器可以';t编码字符'\u20b9';位置248:序号不在范围内(128)

Python UnicodeEncodeError:&x27;ascii';编解码器可以';t编码字符'\u20b9';位置248:序号不在范围内(128),python,python-3.x,beautifulsoup,python-requests,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

我试着制作一个网络刮板来跟踪亚马逊的价格,并在价格发生变化或波动时向我发送电子邮件提醒,但这是我遇到的错误,我对这一点很陌生

详细错误:

    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)
回溯(最近一次呼叫最后一次):
文件“/Users/vaibhav/Desktop/labai/scraper.py”,第53行,在
核对价格
文件“/Users/vaibhav/Desktop/labai/scraper.py”,第20行,检查价格
发送邮件
文件“/Users/vaibhav/Desktop/labai/scraper.py”,第45行,在send_mail中
味精
sendmail中的文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py”,第855行
msg=\u fix\u eols(msg).encode('ascii')
UnicodeEncodeError:“ascii”编解码器无法对位置248处的字符“\u20b9”进行编码:序号不在范围内(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()
导入请求
从bs4导入BeautifulSoup
导入smtplib
URL='1〕https://www.amazon.in/Nokia-Designer-Protective-Printed-Doesnt/dp/B078MFZS9V/ref=bbp_bb_a77114_st_KIqx_w_1?psc=1&smid=A2V1Y4Y0T37MVF'
headers={示例用户代理}
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\u ourprice”).get\u text()
转换价格=浮动(价格[2:5])
如果(换算成300欧元):
发送邮件
def send_mail():
server=smtplib.SMTP('SMTP.gmail.com',587)
server.ehlo()
server.starttls()
server.ehlo()
server.login(“”example@exampleemail'','examplepass')
主题=‘价格下跌’
body='检查amazon链接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',
味精
)
打印('邮件已发送')
server.quit()
核对价格

这是货币符号的结果₹ 卢比不能用ASCII编码。您可能希望为smtplib启用UTF-8(或其他一些unicode编码)。最简单的方法是使用模块

从答案中复制的代码


请注意,在
MIMEText
中,我们使用
'utf-8'
。这就是允许我们对印度卢比货币符号进行编码的原因。

这是货币符号的结果₹ 卢比不能用ASCII编码。您可能希望为smtplib启用UTF-8(或其他一些unicode编码)。最简单的方法是使用模块

从答案中复制的代码


请注意,在
MIMEText
中,我们使用
'utf-8'
。这就是允许我们对INR货币符号进行编码的原因。

为了避免将其作为垃圾邮件关闭,我建议编辑您的帖子以删除特定链接。是否有帮助?问题是您试图发送的消息包含₹, 使用ASCII编码的非ASCII字符。我看不出有任何理由删除网页抓取问题的特定链接。他们是一个很好的选择。垃圾邮件链接(以及包含个人详细信息的链接)是另一个问题,但这似乎不是一个问题。为了避免将其作为垃圾邮件关闭,我建议编辑您的帖子以删除特定链接。是否有帮助?问题是您试图发送的消息包含₹, 使用ASCII编码的非ASCII字符。我看不出有任何理由删除网页抓取问题的特定链接。他们是一个很好的选择。垃圾邮件链接(以及包含个人详细信息的链接)是另一回事,但这似乎不是一回事。
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()