Skip to content

Instantly share code, notes, and snippets.

@oluwaseunladeinde
Forked from AngeloFaella/scraper.py
Last active June 24, 2020 13:31
Show Gist options
  • Save oluwaseunladeinde/e60b08e5259f64e76cf9df5c304b5555 to your computer and use it in GitHub Desktop.
Save oluwaseunladeinde/e60b08e5259f64e76cf9df5c304b5555 to your computer and use it in GitHub Desktop.
Python Amazon Scraper: simple python script to get notified (via email) when the price of a certain product falls below a certain threshold. Full explanation here: https://levelup.gitconnected.com/simple-web-scraping-with-python-1692c11e3b1a
from bs4 import BeautifulSoup
import requests
import smtplib
def check_price() -> int:
# get HTML page
user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 \
(KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36'
headers = {"user-agent": user_agent}
req = requests.get(URL, headers=headers)
# get price
soup = BeautifulSoup(req.text, "html.parser")
span = soup.find("span", {"id": "priceblock_ourprice"}) # <span id="priceblock_ourprice">...</span>
if span:
price = span.text # XY,ZW €
return int(price[:2])
else:
return 0
def send_email(price):
# setup smtp and start connection
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login("your_email_address", "app_password")
# create mail
subject = "Price notification: Mouse Logitech MX 2s"
body = "The price has fallen to EUR " + str(price) + ".\n Check the link: " + URL
msg = f"Subject: {subject}\n\n{body}"
# send mail and quit
server.sendmail("from", "to", msg)
server.quit()
print("Email sent.")
URL = "https://www.amazon.it/gp/product/B072BG9Z8W/"
MY_PRICE = 50
actual_price = check_price()
if actual_price > 0 and actual_price <= MY_PRICE:
send_email(actual_price)
@oluwaseunladeinde
Copy link
Author

Thank you @angelo for this very short useful piece of code. I just forked and made changes to the check_price for situations where the layout of the page is changed or not found. I have also updated the conditions for sending out the mail; only send when the price is greater than zero and less that my price.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment