File size: 2,406 Bytes
c9b8fea
 
9b5b26a
c9b8fea
 
 
 
 
988f727
8fe992b
c7acfba
65158de
9b5b26a
28ba07a
 
f759625
c9b8fea
28ba07a
988f727
c9b8fea
65158de
 
9b5b26a
988f727
 
 
28ba07a
988f727
 
65158de
28ba07a
988f727
28ba07a
988f727
28ba07a
988f727
 
 
 
28ba07a
988f727
 
 
 
 
28ba07a
988f727
 
 
 
 
 
28ba07a
988f727
 
 
28ba07a
988f727
65158de
988f727
 
 
8c01ffb
28ba07a
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
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

@tool
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