Day 47 - Amazon price tracker


Posted by pei_______ on 2022-05-28

learning from 100 Days of Code: The Complete Python Pro Bootcamp for 2022


MY HTTP HEADER


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"))

#Python #課堂筆記 #100 Days of Code







Related Posts

[ js 筆記 ] JS 中的 for ... in 和 for ... of

[ js 筆記 ] JS 中的 for ... in 和 for ... of

[Java] 使用DateTimeFormatterBuilder 解析日期字串

[Java] 使用DateTimeFormatterBuilder 解析日期字串

Deep Learning on 3D object detection paper 閱讀路徑

Deep Learning on 3D object detection paper 閱讀路徑


Comments