Last active
May 12, 2020 15:29
-
-
Save BMU-Verlag/983856882da669f9de41eff41c0e8437 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import smtplib | |
from bs4 import BeautifulSoup | |
URL = 'your_url' | |
TARGET_PRICE = your_price (e.g. 10.0) | |
MAIL_ADDRESS = 'your.mail@gmail.com' | |
PASSWORD = 'generated_password' | |
HEADERS = { | |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36' | |
} | |
def parse_price(price_text): | |
price_text = price_text.replace('€', '') | |
price_text = price_text.replace('EUR', '') | |
price_text = price_text.strip() | |
price_text = price_text.replace('.', '') | |
price_text = price_text.replace(',', '.') | |
return float(price_text) | |
def send_email(name, price): | |
server = smtplib.SMTP('smtp.gmail.com', 587) | |
server.ehlo() | |
server.starttls() | |
server.ehlo() | |
server.login(MAIL_ADDRESS, PASSWORD) | |
subject=f"{name} price below {TARGET_PRICE} €" | |
body=f"The article {name} is available for {price} €. Check it here: {URL}" | |
message = f'Subject: {subject}\n\n{body}' | |
server.sendmail(MAIL_ADDRESS, MAIL_ADDRESS, message.encode('utf8')) | |
server.quit() | |
page = requests.get(URL, headers=HEADERS) | |
soup = BeautifulSoup(page.content, 'html5lib') | |
title_span = soup.find('span', id='productTitle') | |
name = '' | |
if title_span is not None: | |
name = title_span.get_text().strip() | |
price = 0.0 | |
price_span = soup.find('span', id='priceblock_dealprice') | |
if price_span is None: | |
price_span = soup.find('span', id='priceblock_ourprice') | |
if price_span is None: | |
price_span = soup.find('span', class_=a-color-price') | |
if price_span is not None: | |
price = parse_price(price_span.get_text()) | |
if price <= TARGET_PRICE: | |
send_email(name, price) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment