-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #967 from AbhijitMotekar99/main
Added Amazon product availbility checker
- Loading branch information
Showing
2 changed files
with
36 additions
and
0 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
Beginner_Projects/Amazon product availbility checker/amazon.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import requests | ||
from bs4 import BeautifulSoup | ||
|
||
|
||
def check_amazon_availability(product_url): | ||
headers = { | ||
"User-Agent": "Your User Agent Here" # Replace with a valid user agent string | ||
} | ||
|
||
try: | ||
response = requests.get(product_url, headers=headers) | ||
response.raise_for_status() | ||
|
||
soup = BeautifulSoup(response.content, "html.parser") | ||
|
||
title = soup.find("span", {"id": "productTitle"}).get_text(strip=True) | ||
availability = soup.find( | ||
"span", {"class": "a-declarative", "data-asin": True} | ||
).get_text(strip=True) | ||
|
||
if "out of stock" in availability.lower(): | ||
print(f"{title} is currently out of stock on Amazon.") | ||
else: | ||
print(f"{title} is available on Amazon.") | ||
|
||
except requests.exceptions.HTTPError as http_err: | ||
print(f"HTTP error occurred: {http_err}") | ||
except requests.exceptions.RequestException as req_err: | ||
print(f"Request error occurred: {req_err}") | ||
|
||
|
||
if __name__ == "__main__": | ||
product_url = "YOUR_PRODUCT_URL_HERE" | ||
check_amazon_availability(product_url) |
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