WEBScraping TypeError: sendmail() missing 1 required positional argument: 'msg'
WEBScraping TypeError: sendmail() missing 1 required positional argument: 'msg'
我的代码有错误。与 gmail 的连接有问题吗?或者我的代码还有其他问题?
你能告诉我如何解决这个问题吗?
169.9
Garmin Forerunner 735XT GPS Multisport 和 运行 手表,Black/Grey Traceback(最后一次通话):
文件 "C:\Users\User\source\repos\RCS_WEB_SCRAPER\RCS_WEB_SCRAPER\RCS_WEB_SCRAPER.py",第 52 行,在 check_price()
中
文件 "C:\Users\User\source\repos\RCS_WEB_SCRAPER\RCS_WEB_SCRAPER\RCS_WEB_SCRAPER.py",第 29 行,在 check_price send_mail()
中
文件 "C:\Users\User\source\repos\RCS_WEB_SCRAPER\RCS_WEB_SCRAPER\RCS_WEB_SCRAPER.py",第 46 行,在 send_mail msg
中
类型错误: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}\n\n{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
The required arguments are an RFC 822 from-address string, a list of
RFC 822 to-address strings (a bare string will be treated as a list
with 1 address), and a message string.
您需要更新调用 server.sendmail 的代码,以包含发件人地址和收件人地址,然后是您的消息。
我的代码有错误。与 gmail 的连接有问题吗?或者我的代码还有其他问题? 你能告诉我如何解决这个问题吗?
169.9
Garmin Forerunner 735XT GPS Multisport 和 运行 手表,Black/Grey Traceback(最后一次通话):
文件 "C:\Users\User\source\repos\RCS_WEB_SCRAPER\RCS_WEB_SCRAPER\RCS_WEB_SCRAPER.py",第 52 行,在 check_price()
中
文件 "C:\Users\User\source\repos\RCS_WEB_SCRAPER\RCS_WEB_SCRAPER\RCS_WEB_SCRAPER.py",第 29 行,在 check_price send_mail()
中
文件 "C:\Users\User\source\repos\RCS_WEB_SCRAPER\RCS_WEB_SCRAPER\RCS_WEB_SCRAPER.py",第 46 行,在 send_mail msg
中
类型错误: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}\n\n{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
The required arguments are an RFC 822 from-address string, a list of RFC 822 to-address strings (a bare string will be treated as a list with 1 address), and a message string.
您需要更新调用 server.sendmail 的代码,以包含发件人地址和收件人地址,然后是您的消息。