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

Spread Operator 與 Rest Parameters 與 Default Parameters

Spread Operator 與 Rest Parameters 與 Default Parameters

 滲透測試重新打底(5)--bind shell vs. reverse shell

滲透測試重新打底(5)--bind shell vs. reverse shell

[筆記] HTTP 協定

[筆記] HTTP 協定


Comments