Spaces:
Sleeping
Sleeping
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool | |
import datetime | |
import requests | |
import pytz | |
import yaml | |
from tools.final_answer import FinalAnswerTool | |
from Gradio_UI import GradioUI | |
from bs4 import BeautifulSoup | |
def amazon_product_scraper(search_url: str) -> list: | |
""" | |
Scrapes Amazon search results for product titles, prices, delivery fees, and links. | |
Args: | |
search_url: The URL of the Amazon search results page containing product listings. | |
Returns: | |
A list containing two elements: | |
- A list of dictionaries, each with keys "Title", "Price", "Delivery", "Link", sorted by price. | |
- A string containing a recommendation for the best deal. | |
""" | |
headers = { | |
"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" | |
} | |
response = requests.get(search_url, headers=headers) | |
if response.status_code != 200: | |
return [], "Failed to retrieve Amazon results. Amazon may be blocking requests." | |
soup = BeautifulSoup(response.text, 'html.parser') | |
product_data = [] | |
for item in soup.select("div[data-asin]"): | |
title_tag = item.select_one("h2 a") | |
price_tag = item.select_one("span.a-price-whole") | |
delivery_tag = item.select_one("span.s-align-children-center") | |
if title_tag and price_tag: | |
title = title_tag.text.strip() | |
price = price_tag.text.strip().replace(',', '') # Normalize prices | |
delivery = delivery_tag.text.strip() if delivery_tag else "Free" | |
link = "https://www.amazon.com" + title_tag["href"] | |
product_data.append({ | |
"Title": title, | |
"Price": float(price) if price.isnumeric() else None, | |
"Delivery": delivery, | |
"Link": link | |
}) | |
# Filter out products with no price and sort by price | |
product_data = [p for p in product_data if p["Price"] is not None] | |
product_data.sort(key=lambda x: x["Price"]) | |
# Recommendation logic | |
best_deal = product_data[0] if product_data else None | |
recommendation = "" | |
if best_deal is not None: | |
recommendation = f"Best deal: {best_deal['Title']} at ${best_deal['Price']} with {best_deal['Delivery']} (Link: {best_deal['Link']})" | |
return product_data, recommendation | |