WEBScarping TypeError:sendmail()缺少1个必需的位置参数:'msg'

时间:2020-02-25 10:48:06

标签: python-3.x web-scraping beautifulsoup smtplib

我的代码有错误。与gmail连接是否存在问题?还是我的代码还有其他问题? 您能告诉我如何解决此问题吗?

169.9
Garmin Forerunner 735XT GPS Multisport和Running手表,黑色/灰色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()中
send_mail msg中的文件“ C:\ Users \ User \ source \ repos \ RCS_WEB_SCRAPER \ RCS_WEB_SCRAPER \ RCS_WEB_SCRAPER.py”,第46行,
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}\n\n{body}"

    server.sendmail(
        'address@gmail.com',
        msg 
    )
    print('E-mail has been sent!')
    server.quit()

while(True):
    check_price()
    time.sleep(28800)

1 个答案:

答案 0 :(得分:1)

Sendmail要求将3个参数传递给它。发件人地址,收件人地址列表以及要发送的消息。

来自文档https://docs.python.org/3/library/smtplib.html#smtplib.SMTP.sendmail

必需的参数是RFC 822从地址字符串,是 RFC 822地址字符串(裸字符串将被视为列表) 带有1个地址)和一条消息字符串。

您需要在调用server.sendmail的地方更新代码,以包括一个发件人地址和一个收件人地址,然后包括味精。