forked from UTSAVS26/PyVerse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathamazon.py
More file actions
34 lines (25 loc) · 1.11 KB
/
Copy pathamazon.py
File metadata and controls
34 lines (25 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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)