· 6 years ago · Jan 23, 2020, 05:48 PM
1import requests
2from bs4 import BeautifulSoup
3import smtplib
4import time
5
6URL = 'https://www.amazon.ca/gp/product/B07T5S6V3Z/'
7
8headers = {
9 "User-Agent":'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36'}
10
11
12def check_price():
13
14 page = requests.get(URL, headers=headers)
15
16 soup = BeautifulSoup(page.content, 'html;.parser')
17
18 title = soup.find(id="productTitle").get_text()
19 price = soup.find(id="priceblock_ourprice").get_text()
20 converted_price = (price[0:5])
21
22 if(converted_price > 200):
23
24 print(converted_price)
25 print(title.strip())
26
27 if(converted_price > 200):
28 send_mail()
29
30def send_email():
31 server = smtplib.SMTP('smtp.gmail.com',587)
32 server.ehlo()
33 server.starttls()
34 server.ehlo()
35
36 server.login('USERNAME','PASSWORD')
37
38 subject = 'Price went down!'
39 body = 'Check the amazon link https://www.amazon.ca/gp/product/B07T5S6V3Z/'
40
41 msg = f"Subject: {subject}\n\n{body}"
42
43 server.sendmail(
44 '@gmail.com'
45 '@gmail.com'
46 msg
47 )
48 print('Email Sent!')
49
50 server.quit()
51
52 while(True):
53 check_price()
54 time.sleep(3600)