|
| 1 | +import requests |
| 2 | +from bs4 import BeautifulSoup |
| 3 | +import time |
| 4 | +import smtplib |
| 5 | +from email.mime.text import MIMEText |
| 6 | + |
| 7 | +def get_amazon_product_price(product_url): |
| 8 | + headers = { |
| 9 | + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3" |
| 10 | + } |
| 11 | + response = requests.get(product_url, headers=headers) |
| 12 | + |
| 13 | + if response.status_code == 200: |
| 14 | + soup = BeautifulSoup(response.content, 'html.parser') |
| 15 | + price_element = soup.find('span', id='priceblock_ourprice') |
| 16 | + |
| 17 | + if price_element: |
| 18 | + price_text = price_element.get_text() |
| 19 | + return price_text.strip() |
| 20 | + else: |
| 21 | + return "Price not available" |
| 22 | + |
| 23 | + else: |
| 24 | + return "Failed to retrieve data from Amazon" |
| 25 | + |
| 26 | +def send_email(subject, message): |
| 27 | + sender_email = '[email protected]' |
| 28 | + sender_password = 'your_sender_password' |
| 29 | + recipient_email = '[email protected]' |
| 30 | + |
| 31 | + msg = MIMEText(message) |
| 32 | + msg['Subject'] = subject |
| 33 | + msg['From'] = sender_email |
| 34 | + msg['To'] = recipient_email |
| 35 | + |
| 36 | + server = smtplib.SMTP('smtp.gmail.com', 587) |
| 37 | + server.starttls() |
| 38 | + server.login(sender_email, sender_password) |
| 39 | + server.sendmail(sender_email, recipient_email, msg.as_string()) |
| 40 | + server.quit() |
| 41 | + |
| 42 | +if __name__ == "__main__": |
| 43 | + amazon_product_url = "https://www.amazon.com/dp/B07RF1XD36/" |
| 44 | + target_price = 500 # Set your desired target price |
| 45 | + |
| 46 | + while True: |
| 47 | + current_price = get_amazon_product_price(amazon_product_url) |
| 48 | + print(f"Current Price: {current_price}") |
| 49 | + |
| 50 | + if current_price != "Price not available": |
| 51 | + numeric_price = float(current_price.replace('$', '')) |
| 52 | + if numeric_price <= target_price: |
| 53 | + subject = f"Price Alert: Amazon Product Price is now ${numeric_price}" |
| 54 | + message = f"The price of the Amazon product is now ${numeric_price}. You can check it here: {amazon_product_url}" |
| 55 | + send_email(subject, message) |
| 56 | + break |
| 57 | + |
| 58 | + # Check the price every hour |
| 59 | + time.sleep(3600) |
0 commit comments