learning from 100 Days of Code: The Complete Python Pro Bootcamp for 2022
import smtplib
import requests
from bs4 import BeautifulSoup
URL = "https://www.amazon.com/-/zh_TW/SK13-RB-Mr-Coffee%E5%92%96%E5%95%A1%E6%A9%9F/dp/B002YI2IG0/ref=lp_289913_1_5?th=1"
TARGET_PRICE = 20
MY_MAIL = YOUR_OWN_MAIL
PASSWORD = YOUR_OWN_PASSWORD
header = {
"User-Agent": "537.36 (KHTML, like Gecko) Chrome",
"Accept-Language": "zh-TW"
}
response = requests.get(url=URL, headers=header)
html_content = response.text
soup = BeautifulSoup(html_content, "html.parser")
product_name = soup.find(name="span", id="productTitle").getText()[8:-7]
current_price = (float(soup.find(name="span", class_="a-offscreen").getText().split("$")[1]))
print(product_name)
if current_price < TARGET_PRICE:
with smtplib.SMTP("smtp.gmail.com") as connect:
connect.starttls()
connect.login(user=MY_MAIL, password=PASSWORD)
connect.sendmail(from_addr=MY_MAIL,
to_addrs= YOUR TARGET MAIL,
msg=f"Subject:Lower Price alert!\n\nYour tacking item: {product_name}."
f"Now is just ${current_price}!\nURL= {URL}".format().encode("utf-8"))