Amazing-Python-Scripts
62 строки · 2.0 Кб
1import requests
2from bs4 import BeautifulSoup
3import time
4import smtplib
5from email.mime.text import MIMEText
6
7
8def get_amazon_product_price(product_url):
9headers = {
10"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"
11}
12response = requests.get(product_url, headers=headers)
13
14if response.status_code == 200:
15soup = BeautifulSoup(response.content, 'html.parser')
16price_element = soup.find('span', id='priceblock_ourprice')
17
18if price_element:
19price_text = price_element.get_text()
20return price_text.strip()
21else:
22return "Price not available"
23
24else:
25return "Failed to retrieve data from Amazon"
26
27
28def send_email(subject, message):
29sender_email = 'your_sender_email@gmail.com'
30sender_password = 'your_sender_password'
31recipient_email = 'recipient_email@example.com'
32
33msg = MIMEText(message)
34msg['Subject'] = subject
35msg['From'] = sender_email
36msg['To'] = recipient_email
37
38server = smtplib.SMTP('smtp.gmail.com', 587)
39server.starttls()
40server.login(sender_email, sender_password)
41server.sendmail(sender_email, recipient_email, msg.as_string())
42server.quit()
43
44
45if __name__ == "__main__":
46amazon_product_url = "https://www.amazon.com/dp/B07RF1XD36/"
47target_price = 500 # Set your desired target price
48
49while True:
50current_price = get_amazon_product_price(amazon_product_url)
51print(f"Current Price: {current_price}")
52
53if current_price != "Price not available":
54numeric_price = float(current_price.replace('$', ''))
55if numeric_price <= target_price:
56subject = f"Price Alert: Amazon Product Price is now ${numeric_price}"
57message = f"The price of the Amazon product is now ${numeric_price}. You can check it here: {amazon_product_url}"
58send_email(subject, message)
59break
60
61# Check the price every hour
62time.sleep(3600)
63