Implementation:24mlight A Share investment Agent TavilyNewsSearch search
| Knowledge Sources | |
|---|---|
| Domains | Web_Scraping, Data_Engineering |
| Last Updated | 2026-10-10 14:00 GMT |
Overview
Concrete tool for Tavily API news search with automatic API key rotation, payload construction, and result parsing, provided by the A_Share_investment_Agent repository.
Description
The TavilyNewsSearch class implements the NewsSearchProvider abstract interface for the Tavily news search API. It collects API keys from constructor arguments and environment variables (TAVILY_API_KEY, TAVILY_API_KEYS), rotates through keys on failure, and parses raw API responses into structured NewsSearchResult dataclass instances.
Key methods:
__init__: Collects and deduplicates API keys from args and envis_available: Returns True if any keys configuredsearch: Executes search with key rotation_build_payload: Builds Tavily request (topic="news", max_results capped at 10)_parse_results: Parses Tavily response into NewsSearchResult objects
Usage
Import when you need to:
- Search for news via the Tavily API
- Support API key rotation for reliability
Code Reference
Source Location
- Repository: A_Share_investment_Agent
- File: src/crawler/news_search/tavily_impl.py
- Lines: L24-178 (class), L77-120 (search), L122-150 (_build_payload), L152-178 (_parse_results)
Signature
class TavilyNewsSearch(NewsSearchProvider):
def __init__(
self,
api_key: Optional[str] = None,
api_keys: Optional[List[str]] = None,
timeout_seconds: int = 30,
):
"""Initialize with API keys from args and environment."""
def is_available(self) -> bool:
"""Check if Tavily is available (has API keys)."""
def search(self, query: SearchQuery) -> List[NewsSearchResult]:
"""Execute Tavily search with key rotation."""
def _build_payload(self, query: SearchQuery) -> Dict[str, Any]:
"""Build Tavily API request payload."""
def _parse_results(self, raw_results: List[Any]) -> List[NewsSearchResult]:
"""Parse Tavily API response into NewsSearchResult objects."""
Import
from src.crawler.news_search.tavily_impl import TavilyNewsSearch
from src.crawler.news_search.base import SearchQuery, NewsSearchResult, convert_to_news_format
External Reference
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| query | SearchQuery | Yes | Dataclass with query string, max_results, topic, days_back, start_date, end_date |
Outputs
| Name | Type | Description |
|---|---|---|
| return | List[NewsSearchResult] | Results with title, url, content, source, published_date, score |
Usage Examples
Basic Search
from src.crawler.news_search.tavily_impl import TavilyNewsSearch
from src.crawler.news_search.base import SearchQuery, convert_to_news_format
search = TavilyNewsSearch()
if search.is_available():
query = SearchQuery(
query="贵州茅台 股票新闻",
max_results=10,
days_back=7,
)
results = search.search(query)
news_list = convert_to_news_format(results, symbol="600519")
With Date Range
query = SearchQuery(
query="沪深300 宏观新闻",
max_results=10,
start_date="2025-01-08",
end_date="2025-01-15",
)
results = search.search(query)