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