Skip to content

Instantly share code, notes, and snippets.

@SebastianRedinger
Created January 25, 2020 18:46
Show Gist options
  • Save SebastianRedinger/4ef0e91657a150fb694c79ea942afa0d to your computer and use it in GitHub Desktop.
Save SebastianRedinger/4ef0e91657a150fb694c79ea942afa0d to your computer and use it in GitHub Desktop.
Python: AmazonPriceChecker
import requests
from bs4 import BeautifulSoup
import smtplib
import time
url = 'YOUR_AMAZON_URL_HERE'
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.4 Safari/605.1.15'}
def check_price():
page = requests.get(url, headers=headers)
soup = BeautifulSoup(page.content, 'html.parser')
title = soup.find(id='productTitle').get_text().strip()
price = float(soup.find(id='priceblock_ourprice').get_text()[0:5].replace(',', '.'))
print('current price: ' + str(price))
if(price < 29.99):
#send mail
send_mail(title, price)
def send_mail(title, price):
server = smtplib.SMTP('mail.your-server.de', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login('sender@mail.com', 'YOUR_PASSWORD')
subject = 'Price is low'
body = 'Price: ' + str(price) + '\n\n Product: ' + title
msg = f'Subject: {subject}\n\n{body}'
server.sendmail(
'sender@mail.com',
'receiver@mail.com',
msg
)
print('Message has been sent')
while(True):
check_price()
#Check every 12 hours
time.sleep(43200)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment