codewithdark commited on
Commit
18b290f
·
verified ·
1 Parent(s): 630a416

Delete main.py

Browse files
Files changed (1) hide show
  1. main.py +0 -146
main.py DELETED
@@ -1,146 +0,0 @@
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()