我的代码中有一个错误。与gmail的连接有问题吗?或者我的代码有其他问题?你能告诉我如何解决这个问题吗?
169.9
Garmin Forerunner 735XT GPS多端口和跑步手表,黑色/灰色追踪(最后一次通话(:
文件"C:\Users\User\source\repos\RCS_WEB_SRCAPER\RCS_WB_SCRAPER.py",第52行,在check_price((中
,在check_price send_mail((中
文件"C:\Users\User\source\repos\RCS_WEB_SRCAPER\RCS_WAB_SRCAPer.py",第46行,在send_mail msg中
TypeError:sendmail((缺少1个必需的位置参数:"msg">
我的代码
import requests
from bs4 import BeautifulSoup
import smtplib
import time
URL = 'https://www.amazon.co.uk/Garmin-Forerunner-735XT-Multisport-Running-Black-Grey/dp/B01DWIY39A/ref=sr_1_3?keywords=garmin&qid=1582615813&sr=8-3'
headers = {
"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0'}
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_dealprice").get_text()
converted_price = float(price[1:6])
if(converted_price < 160.00):
send_mail()
print(converted_price)
print(title.strip())
if(converted_price > 160.00):
send_mail()
def send_mail():
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login('address', 'mAJnkzjfTqw8xJe')
subject = 'Price decreased!'
body = 'Now it is time to buy: https://www.amazon.co.uk/Garmin-Forerunner-735XT-Multisport-Running-Black-Grey/dp/B01DWIY39A/ref=sr_1_3?keywords=garmin&qid=1582615813&sr=8-3'
msg = f"Subject: {subject}nn{body}"
server.sendmail(
'address@gmail.com',
msg
)
print('E-mail has been sent!')
server.quit()
while(True):
check_price()
time.sleep(28800)
Sendmail需要3个参数才能传递给它。一个发件人地址、一个收件人地址列表和一条要发送的消息。
从文档中https://docs.python.org/3/library/smtplib.html#smtplib.SMTP.sendmail
所需的参数是来自地址字符串的RFC 822RFC 822寻址字符串(裸字符串将被视为列表具有1个地址(和消息字符串。
您需要更新您调用server.sendmail的代码,以包括发件人地址和收件人地址,然后是您的消息。