|
from smolagents import Tool |
|
from typing import Any, Optional |
|
|
|
class SimpleTool(Tool): |
|
name = "fetch_lastest_news_urls_and_titles" |
|
description = "This tool extracts the titles and URLs of the latest news articles from a news website's homepage." |
|
inputs = {"url":{"type":"string","description":"The URL of the news website's homepage."}} |
|
output_type = "array" |
|
|
|
def forward(self, url: str) -> list[tuple[str, str]]: |
|
""" |
|
This tool extracts the titles and URLs of the latest news articles from a news website's homepage. |
|
|
|
Args: |
|
url (str): The URL of the news website's homepage. |
|
|
|
Returns: |
|
list[tuple[str, str]]: A list of titles and URLs of the latest news articles. |
|
""" |
|
import requests |
|
from bs4 import BeautifulSoup |
|
|
|
response = requests.get(url) |
|
soup = BeautifulSoup(response.text, 'html.parser') |
|
urls = [] |
|
titles = [] |
|
for article in soup.find_all('article'): |
|
title = article.find('h3', class_='title-news') |
|
if title: |
|
title = title.text.strip() |
|
url = article.find('a')['href'] |
|
urls.append(url) |
|
titles.append(title) |
|
return list(zip(titles, urls)) |