From a3c9557573e4fc7de0f0c2b30b192625c4d44244 Mon Sep 17 00:00:00 2001 From: Shreyas Prabhu Date: Thu, 20 Oct 2022 11:23:16 +0530 Subject: [PATCH] scraper for amazon --- amzn.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 amzn.py diff --git a/amzn.py b/amzn.py new file mode 100644 index 0000000..dc6ac10 --- /dev/null +++ b/amzn.py @@ -0,0 +1,30 @@ +from bs4 import BeautifulSoup +import requests + +PRODUCT_URI = input("Enter product url: ") +price = 0 + + +def get_product_price(url): + response = requests.get( + url, + headers={ + "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36" + }, + ) + soup = BeautifulSoup(response.text, "lxml") + available = soup.find("div", {"id": "availability"}) + if available is not None: + if "Currently unavailable." in available.text: + print("Product is currently unavailable.") + return False + price_parent = soup.find( + "span", {"class": "a-price a-text-price a-size-medium apexPriceToPay"} + ) + if price_parent is not None: + price = price_parent.text.split("₹")[1] + print("Price: ₹", price) + return True + + +get_product_price(PRODUCT_URI)