forked from mannekeenpis/100-days-of-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
41 lines (31 loc) · 1.18 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import requests
import lxml
import smtplib
from bs4 import BeautifulSoup
YOUR_EMAIL = YOUR_EMAIL
YOUR_PASSWORD = YOUR_PASSWORD
url = "https://www.amazon.com/Instant-Pot-Duo-Evo-Plus/dp/B07W55DDFB/ref=sr_1_1?qid=1597662463"
header = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 12_1_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/9.8.4 Safari/602.8.7",
"Accept-Language": "en-GB,en-US;q=0.9,en;q=0.8"
}
response = requests.get(url, headers=header)
soup = BeautifulSoup(response.content, "lxml")
print(soup.prettify())
price = soup.find(id="priceblock_ourprice").get_text()
price_without_currency = price.split("$")[1]
price_as_float = float(price_without_currency)
print(price_as_float)
title = soup.find(id="productTitle").get_text().strip()
print(title)
BUY_PRICE = 45.81
if price_as_float < BUY_PRICE:
message = f"{title} is now {price}"
with smtplib.SMTP("smtp.gmail.com", port=587) as connection:
connection.starttls()
result = connection.login(YOUR_EMAIL, YOUR_PASSWORD)
connection.sendmail(
from_addr=YOUR_EMAIL,
to_addrs=YOUR_EMAIL,
msg=f"Subject:Amazon Price Alert!\n\n{message}\n{url}"
)