Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,8 +5,18 @@ import pandas as pd
|
|
5 |
import yaml
|
6 |
from tools.final_answer import FinalAnswerTool
|
7 |
|
8 |
-
@tool
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
"""
|
11 |
Scrapes Amazon search results for product titles, prices, delivery fees, and links.
|
12 |
|
@@ -14,9 +24,9 @@ def amazon_product_scraper(search_url: str) -> tuple[pd.DataFrame, str]:
|
|
14 |
search_url (str): The URL of the Amazon search results page. This should be a valid Amazon search URL containing product listings.
|
15 |
|
16 |
Returns:
|
17 |
-
|
18 |
-
- A
|
19 |
-
- A recommendation
|
20 |
"""
|
21 |
headers = {
|
22 |
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"
|
@@ -24,7 +34,7 @@ def amazon_product_scraper(search_url: str) -> tuple[pd.DataFrame, str]:
|
|
24 |
|
25 |
response = requests.get(search_url, headers=headers)
|
26 |
if response.status_code != 200:
|
27 |
-
return
|
28 |
|
29 |
soup = BeautifulSoup(response.text, 'html.parser')
|
30 |
|
@@ -52,16 +62,13 @@ def amazon_product_scraper(search_url: str) -> tuple[pd.DataFrame, str]:
|
|
52 |
product_data = [p for p in product_data if p["Price"] is not None]
|
53 |
product_data.sort(key=lambda x: x["Price"])
|
54 |
|
55 |
-
# Convert to DataFrame for better visualization
|
56 |
-
df = pd.DataFrame(product_data)
|
57 |
-
|
58 |
# Recommendation logic
|
59 |
-
best_deal =
|
60 |
recommendation = ""
|
61 |
if best_deal is not None:
|
62 |
recommendation = f"Best deal: {best_deal['Title']} at ${best_deal['Price']} with {best_deal['Delivery']} (Link: {best_deal['Link']})"
|
63 |
|
64 |
-
return
|
65 |
|
66 |
# Define the Agent
|
67 |
final_answer = FinalAnswerTool()
|
|
|
5 |
import yaml
|
6 |
from tools.final_answer import FinalAnswerTool
|
7 |
|
8 |
+
@tool(
|
9 |
+
name="amazon_product_scraper",
|
10 |
+
description="Scrapes Amazon search results for product titles, prices, delivery fees, and links, then returns a sorted list of products based on price.",
|
11 |
+
arguments=[
|
12 |
+
{"name": "search_url", "type": "str", "description": "The URL of the Amazon search results page."}
|
13 |
+
],
|
14 |
+
outputs=[
|
15 |
+
{"name": "products", "type": "list", "description": "A list of dictionaries with product details sorted by price."},
|
16 |
+
{"name": "recommendation", "type": "str", "description": "The best deal based on price and delivery fees."}
|
17 |
+
]
|
18 |
+
)
|
19 |
+
def amazon_product_scraper(search_url: str) -> list:
|
20 |
"""
|
21 |
Scrapes Amazon search results for product titles, prices, delivery fees, and links.
|
22 |
|
|
|
24 |
search_url (str): The URL of the Amazon search results page. This should be a valid Amazon search URL containing product listings.
|
25 |
|
26 |
Returns:
|
27 |
+
list: A list containing two elements:
|
28 |
+
- A list of dictionaries, each with keys "Title", "Price", "Delivery", "Link", sorted by price.
|
29 |
+
- A string containing a recommendation for the best deal.
|
30 |
"""
|
31 |
headers = {
|
32 |
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"
|
|
|
34 |
|
35 |
response = requests.get(search_url, headers=headers)
|
36 |
if response.status_code != 200:
|
37 |
+
return [], "Failed to retrieve Amazon results. Amazon may be blocking requests."
|
38 |
|
39 |
soup = BeautifulSoup(response.text, 'html.parser')
|
40 |
|
|
|
62 |
product_data = [p for p in product_data if p["Price"] is not None]
|
63 |
product_data.sort(key=lambda x: x["Price"])
|
64 |
|
|
|
|
|
|
|
65 |
# Recommendation logic
|
66 |
+
best_deal = product_data[0] if product_data else None
|
67 |
recommendation = ""
|
68 |
if best_deal is not None:
|
69 |
recommendation = f"Best deal: {best_deal['Title']} at ${best_deal['Price']} with {best_deal['Delivery']} (Link: {best_deal['Link']})"
|
70 |
|
71 |
+
return product_data, recommendation
|
72 |
|
73 |
# Define the Agent
|
74 |
final_answer = FinalAnswerTool()
|