Amazing-Python-Scripts

Форк
0
62 строки · 2.0 Кб
1
import requests
2
from bs4 import BeautifulSoup
3
import time
4
import smtplib
5
from email.mime.text import MIMEText
6

7

8
def get_amazon_product_price(product_url):
9
    headers = {
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
    }
12
    response = requests.get(product_url, headers=headers)
13

14
    if response.status_code == 200:
15
        soup = BeautifulSoup(response.content, 'html.parser')
16
        price_element = soup.find('span', id='priceblock_ourprice')
17

18
        if price_element:
19
            price_text = price_element.get_text()
20
            return price_text.strip()
21
        else:
22
            return "Price not available"
23

24
    else:
25
        return "Failed to retrieve data from Amazon"
26

27

28
def send_email(subject, message):
29
    sender_email = 'your_sender_email@gmail.com'
30
    sender_password = 'your_sender_password'
31
    recipient_email = 'recipient_email@example.com'
32

33
    msg = MIMEText(message)
34
    msg['Subject'] = subject
35
    msg['From'] = sender_email
36
    msg['To'] = recipient_email
37

38
    server = smtplib.SMTP('smtp.gmail.com', 587)
39
    server.starttls()
40
    server.login(sender_email, sender_password)
41
    server.sendmail(sender_email, recipient_email, msg.as_string())
42
    server.quit()
43

44

45
if __name__ == "__main__":
46
    amazon_product_url = "https://www.amazon.com/dp/B07RF1XD36/"
47
    target_price = 500  # Set your desired target price
48

49
    while True:
50
        current_price = get_amazon_product_price(amazon_product_url)
51
        print(f"Current Price: {current_price}")
52

53
        if current_price != "Price not available":
54
            numeric_price = float(current_price.replace('$', ''))
55
            if numeric_price <= target_price:
56
                subject = f"Price Alert: Amazon Product Price is now ${numeric_price}"
57
                message = f"The price of the Amazon product is now ${numeric_price}. You can check it here: {amazon_product_url}"
58
                send_email(subject, message)
59
                break
60

61
        # Check the price every hour
62
        time.sleep(3600)
63

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.