Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
def get_search_results(search_query):
|
11 |
+
try:
|
12 |
+
url = f"https://www.amazon.com/s?k={search_query}"
|
13 |
+
|
14 |
+
ua = UserAgent(browsers=['Safari', 'edge', 'Google Chrome', 'UC Browser', 'opera', 'Mozilla Firefox', 'Brave'])
|
15 |
+
|
16 |
+
headers = {
|
17 |
+
"User-Agent": ua.random,
|
18 |
+
"Accept-Language": "en-GB,en-US;q=0.9,en;q=0.8"
|
19 |
+
}
|
20 |
+
|
21 |
+
response = requests.get(url, headers=headers)
|
22 |
+
response.raise_for_status()
|
23 |
+
|
24 |
+
logging.info(f"Response status code: {response.status_code}")
|
25 |
+
|
26 |
+
soup = BeautifulSoup(response.content, "html.parser")
|
27 |
+
return soup
|
28 |
+
except requests.RequestException as e:
|
29 |
+
logging.error(f"Error fetching search results: {e}")
|
30 |
+
return None
|
31 |
+
|
32 |
+
|
33 |
+
def extract_product_info(search_results):
|
34 |
+
try:
|
35 |
+
products = []
|
36 |
+
results = search_results.find_all("div", class_="s-result-item")
|
37 |
+
for result in results:
|
38 |
+
title_element = result.find("span", class_="a-size-medium")
|
39 |
+
price_element = result.find("span", class_="a-price")
|
40 |
+
image_element = result.find("img", class_="s-image")
|
41 |
+
review_count_element = result.find("span", class_="a-size-base")
|
42 |
+
deal_element = result.find("span", class_="a-badge-text")
|
43 |
+
|
44 |
+
if title_element and price_element and image_element:
|
45 |
+
title = title_element.get_text().strip()
|
46 |
+
price = price_element.find("span", class_="a-offscreen").get_text().strip()
|
47 |
+
image_url = image_element["src"]
|
48 |
+
link = result.find("a", class_="a-link-normal")["href"]
|
49 |
+
reviews = review_count_element.get_text().strip() if review_count_element else "No reviews"
|
50 |
+
is_deal = bool(deal_element) # Check if deal_element exists
|
51 |
+
products.append(
|
52 |
+
{"title": title, "price": price, "image_url": image_url, "link": link, "reviews": reviews,
|
53 |
+
"is_deal": is_deal})
|
54 |
+
|
55 |
+
|
56 |
+
except Exception as e:
|
57 |
+
logging.error(f"Error extracting product info: {e}")
|
58 |
+
return []
|
59 |
+
|
60 |
+
return products
|
61 |
+
|
62 |
+
def main():
|
63 |
+
try:
|
64 |
+
st.title("Amazon Product Search")
|
65 |
+
|
66 |
+
page = st.radio("Navigate", ["Home", "Search Items"])
|
67 |
+
|
68 |
+
st.markdown("-----")
|
69 |
+
if page == "Home":
|
70 |
+
# Fetch and display products for a random item category
|
71 |
+
random_item_names = [
|
72 |
+
"Laptops",
|
73 |
+
"Computer Monitors",
|
74 |
+
"Computer Networking",
|
75 |
+
"Computer Servers",
|
76 |
+
"Computer Components",
|
77 |
+
"Computer Accessories",
|
78 |
+
"Computer Peripherals",
|
79 |
+
"External Hard Drives",
|
80 |
+
"Solid State Drives",
|
81 |
+
"Graphics Cards",
|
82 |
+
"RAM Memory",
|
83 |
+
"Processors",
|
84 |
+
"Keyboards",
|
85 |
+
"Mice",
|
86 |
+
"Webcams",
|
87 |
+
"Headsets",
|
88 |
+
"Printers",
|
89 |
+
"Scanners",
|
90 |
+
"Projectors",
|
91 |
+
"macbook", "iphone",
|
92 |
+
"samsung", "phone",
|
93 |
+
"galaxy notebook"
|
94 |
+
]
|
95 |
+
|
96 |
+
num_items = random.randint(8, 12)
|
97 |
+
selected_item_names = random.sample(random_item_names, num_items)
|
98 |
+
|
99 |
+
for item_name in selected_item_names:
|
100 |
+
search_results = get_search_results(item_name)
|
101 |
+
products = extract_product_info(search_results)
|
102 |
+
if products:
|
103 |
+
for idx, product in enumerate(products, start=1):
|
104 |
+
col1, col2 = st.columns([1, 3])
|
105 |
+
with col1:
|
106 |
+
st.image(product['image_url'])
|
107 |
+
with col2:
|
108 |
+
st.markdown(f"{product['title']}")
|
109 |
+
st.subheader(f"{product['price']}")
|
110 |
+
st.write(f"**Reviews:** {product['reviews']}")
|
111 |
+
st.write("Deal Available" if product['is_deal'] else "No Deal Available")
|
112 |
+
st.link_button("View on Amazon", f"https://www.amazon.com{product['link']}")
|
113 |
+
st.markdown("---")
|
114 |
+
else:
|
115 |
+
st.write(f"No products found for '{item_name}'.")
|
116 |
+
|
117 |
+
elif page == "Search Items":
|
118 |
+
# Display search input and results
|
119 |
+
search_query = st.text_input("Enter your search query:")
|
120 |
+
if search_query:
|
121 |
+
search_results = get_search_results(search_query)
|
122 |
+
products = extract_product_info(search_results)
|
123 |
+
if products:
|
124 |
+
# Display the search results
|
125 |
+
st.title("Search Results:")
|
126 |
+
for idx, product in enumerate(products, start=1):
|
127 |
+
col1, col2 = st.columns([1, 3])
|
128 |
+
with col1:
|
129 |
+
st.image(product['image_url'])
|
130 |
+
with col2:
|
131 |
+
st.markdown(f"{product['title']}")
|
132 |
+
st.subheader(f"{product['price']}")
|
133 |
+
st.write(f"**Reviews:** {product['reviews']}")
|
134 |
+
st.write("Deal Available" if product['is_deal'] else "No Deal Available")
|
135 |
+
st.link_button("View on Amazon", f"https://www.amazon.com{product['link']}")
|
136 |
+
st.markdown("---")
|
137 |
+
else:
|
138 |
+
st.write(f"No products found for '{search_query}'.")
|
139 |
+
|
140 |
+
|
141 |
+
except Exception as e:
|
142 |
+
st.error(f"An error occurred: {e}")
|
143 |
+
|
144 |
+
if __name__ == "__main__":
|
145 |
+
main()
|