Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- README.md +49 -13
- main.py +146 -0
- requirements.txt +0 -0
README.md
CHANGED
@@ -1,13 +1,49 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
# Amazon Product Search App
|
3 |
+
|
4 |
+
This is a Streamlit web application that allows users to search for products on Amazon and display the search results. Users can either view random products from various categories or search for specific products by entering a search query.
|
5 |
+
|
6 |
+
## Features
|
7 |
+
|
8 |
+
- **Home Page**: Displays random products from various categories.
|
9 |
+
- **Search Page**: Allows users to search for specific products by entering a search query.
|
10 |
+
- **Product Information**: Shows product images, titles, prices, reviews, and whether it's a deal of the day.
|
11 |
+
- **View on Amazon**: Provides a direct link to view the product on Amazon's website.
|
12 |
+
- **Exception Handling**: Provides error messages for various exceptions such as connection errors.
|
13 |
+
|
14 |
+
## Usage
|
15 |
+
|
16 |
+
1. Clone the repository:
|
17 |
+
|
18 |
+
```bash
|
19 |
+
git clone https://github.com/codewithdark-git/amazon-product-search.git
|
20 |
+
```
|
21 |
+
|
22 |
+
2. Install the required dependencies:
|
23 |
+
|
24 |
+
```bash
|
25 |
+
pip install -r requirements.txt
|
26 |
+
```
|
27 |
+
|
28 |
+
3. Run the Streamlit app:
|
29 |
+
|
30 |
+
```bash
|
31 |
+
streamlit run app.py
|
32 |
+
```
|
33 |
+
|
34 |
+
4. Open your web browser and go to `http://localhost:8501` to access the app.
|
35 |
+
|
36 |
+
## Dependencies
|
37 |
+
|
38 |
+
- **requests**: For sending HTTP requests to Amazon's website.
|
39 |
+
- **Beautiful Soup**: For parsing HTML content.
|
40 |
+
- **Streamlit**: For building the web application.
|
41 |
+
- **urllib**: For URL parsing.
|
42 |
+
|
43 |
+
## Credits
|
44 |
+
|
45 |
+
This app was created by Dark Coder.
|
46 |
+
|
47 |
+
## License
|
48 |
+
|
49 |
+
This project is licensed under the [MIT License](LICENSE).
|
main.py
ADDED
@@ -0,0 +1,146 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
from bs4 import BeautifulSoup
|
3 |
+
import streamlit as st
|
4 |
+
import random
|
5 |
+
import logging
|
6 |
+
from fake_useragent import UserAgent
|
7 |
+
|
8 |
+
logging.basicConfig(level=logging.INFO)
|
9 |
+
|
10 |
+
|
11 |
+
def get_search_results(search_query):
|
12 |
+
try:
|
13 |
+
url = f"https://www.amazon.com/s?k={search_query}"
|
14 |
+
|
15 |
+
ua = UserAgent(browsers=['Safari', 'edge', 'Google Chrome', 'UC Browser', 'opera', 'Mozilla Firefox', 'Brave'])
|
16 |
+
|
17 |
+
headers = {
|
18 |
+
"User-Agent": ua.random,
|
19 |
+
"Accept-Language": "en-GB,en-US;q=0.9,en;q=0.8"
|
20 |
+
}
|
21 |
+
|
22 |
+
response = requests.get(url, headers=headers)
|
23 |
+
response.raise_for_status()
|
24 |
+
|
25 |
+
logging.info(f"Response status code: {response.status_code}")
|
26 |
+
|
27 |
+
soup = BeautifulSoup(response.content, "html.parser")
|
28 |
+
return soup
|
29 |
+
except requests.RequestException as e:
|
30 |
+
logging.error(f"Error fetching search results: {e}")
|
31 |
+
return None
|
32 |
+
|
33 |
+
|
34 |
+
def extract_product_info(search_results):
|
35 |
+
try:
|
36 |
+
products = []
|
37 |
+
results = search_results.find_all("div", class_="s-result-item")
|
38 |
+
for result in results:
|
39 |
+
title_element = result.find("span", class_="a-size-medium")
|
40 |
+
price_element = result.find("span", class_="a-price")
|
41 |
+
image_element = result.find("img", class_="s-image")
|
42 |
+
review_count_element = result.find("span", class_="a-size-base")
|
43 |
+
deal_element = result.find("span", class_="a-badge-text")
|
44 |
+
|
45 |
+
if title_element and price_element and image_element:
|
46 |
+
title = title_element.get_text().strip()
|
47 |
+
price = price_element.find("span", class_="a-offscreen").get_text().strip()
|
48 |
+
image_url = image_element["src"]
|
49 |
+
link = result.find("a", class_="a-link-normal")["href"]
|
50 |
+
reviews = review_count_element.get_text().strip() if review_count_element else "No reviews"
|
51 |
+
is_deal = bool(deal_element) # Check if deal_element exists
|
52 |
+
products.append(
|
53 |
+
{"title": title, "price": price, "image_url": image_url, "link": link, "reviews": reviews,
|
54 |
+
"is_deal": is_deal})
|
55 |
+
|
56 |
+
|
57 |
+
except Exception as e:
|
58 |
+
logging.error(f"Error extracting product info: {e}")
|
59 |
+
return []
|
60 |
+
|
61 |
+
return products
|
62 |
+
|
63 |
+
def main():
|
64 |
+
try:
|
65 |
+
st.title("Amazon Product Search")
|
66 |
+
|
67 |
+
page = st.radio("Navigate", ["Home", "Search Items"])
|
68 |
+
|
69 |
+
st.markdown("-----")
|
70 |
+
if page == "Home":
|
71 |
+
# Fetch and display products for a random item category
|
72 |
+
random_item_names = [
|
73 |
+
"Laptops",
|
74 |
+
"Computer Monitors",
|
75 |
+
"Computer Networking",
|
76 |
+
"Computer Servers",
|
77 |
+
"Computer Components",
|
78 |
+
"Computer Accessories",
|
79 |
+
"Computer Peripherals",
|
80 |
+
"External Hard Drives",
|
81 |
+
"Solid State Drives",
|
82 |
+
"Graphics Cards",
|
83 |
+
"RAM Memory",
|
84 |
+
"Processors",
|
85 |
+
"Keyboards",
|
86 |
+
"Mice",
|
87 |
+
"Webcams",
|
88 |
+
"Headsets",
|
89 |
+
"Printers",
|
90 |
+
"Scanners",
|
91 |
+
"Projectors",
|
92 |
+
"macbook", "iphone",
|
93 |
+
"samsung", "phone",
|
94 |
+
"galaxy notebook"
|
95 |
+
]
|
96 |
+
|
97 |
+
num_items = random.randint(8, 12)
|
98 |
+
selected_item_names = random.sample(random_item_names, num_items)
|
99 |
+
|
100 |
+
for item_name in selected_item_names:
|
101 |
+
search_results = get_search_results(item_name)
|
102 |
+
products = extract_product_info(search_results)
|
103 |
+
if products:
|
104 |
+
for idx, product in enumerate(products, start=1):
|
105 |
+
col1, col2 = st.columns([1, 3])
|
106 |
+
with col1:
|
107 |
+
st.image(product['image_url'])
|
108 |
+
with col2:
|
109 |
+
st.markdown(f"{product['title']}")
|
110 |
+
st.subheader(f"{product['price']}")
|
111 |
+
st.write(f"**Reviews:** {product['reviews']}")
|
112 |
+
st.write("Deal Available" if product['is_deal'] else "No Deal Available")
|
113 |
+
st.link_button("View on Amazon", f"https://www.amazon.com{product['link']}")
|
114 |
+
st.markdown("---")
|
115 |
+
else:
|
116 |
+
st.write(f"No products found for '{item_name}'.")
|
117 |
+
|
118 |
+
elif page == "Search Items":
|
119 |
+
# Display search input and results
|
120 |
+
search_query = st.text_input("Enter your search query:")
|
121 |
+
if search_query:
|
122 |
+
search_results = get_search_results(search_query)
|
123 |
+
products = extract_product_info(search_results)
|
124 |
+
if products:
|
125 |
+
# Display the search results
|
126 |
+
st.title("Search Results:")
|
127 |
+
for idx, product in enumerate(products, start=1):
|
128 |
+
col1, col2 = st.columns([1, 3])
|
129 |
+
with col1:
|
130 |
+
st.image(product['image_url'])
|
131 |
+
with col2:
|
132 |
+
st.markdown(f"{product['title']}")
|
133 |
+
st.subheader(f"{product['price']}")
|
134 |
+
st.write(f"**Reviews:** {product['reviews']}")
|
135 |
+
st.write("Deal Available" if product['is_deal'] else "No Deal Available")
|
136 |
+
st.link_button("View on Amazon", f"https://www.amazon.com{product['link']}")
|
137 |
+
st.markdown("---")
|
138 |
+
else:
|
139 |
+
st.write(f"No products found for '{search_query}'.")
|
140 |
+
|
141 |
+
|
142 |
+
except Exception as e:
|
143 |
+
st.error(f"An error occurred: {e}")
|
144 |
+
|
145 |
+
if __name__ == "__main__":
|
146 |
+
main()
|
requirements.txt
ADDED
Binary file (1.62 kB). View file
|
|