Implementation:666ghj BettaFish NewsCollector collect and save
| Knowledge Sources | |
|---|---|
| Domains | Web_Crawling, News_Collection |
| Last Updated | 2026-10-08 00:00 GMT |
Overview
Concrete collector class that fetches trending news from 12+ Chinese platforms via a public API and saves them to a database for topic extraction.
Description
The NewsCollector class fetches hot news from 12 Chinese news sources (微博热搜, 知乎热榜, B站热搜, 抖音热榜, 头条, etc.) via the `tophub.today` API. It uses `httpx.AsyncClient` for concurrent HTTP requests, handles timeouts and errors gracefully, and saves collected news to the `daily_news` database table via a DatabaseManager.
Usage
Import this class when you need to collect trending news for topic extraction. It is the first step in the MindSpider BroadTopicExtraction workflow.
Code Reference
Source Location
- Repository: BettaFish
- File: `MindSpider/BroadTopicExtraction/get_today_news.py:L45-308`
Signature
class NewsCollector:
def __init__(self):
"""Initialize with DatabaseManager and supported source list."""
async def fetch_news(self, source: str) -> dict:
"""Fetch news from a single source via tophub.today API."""
async def get_popular_news(self, sources: List[str] = None) -> List[dict]:
"""Fetch news from all or specified sources."""
async def collect_and_save_news(
self, sources: Optional[List[str]] = None
) -> Dict:
"""
Collect and save news to database.
Args:
sources: Optional list of source IDs (None = all 12 sources)
Returns:
Dict with news_list, total_news, successful_sources, saved_count
"""
async def get_today_news(self) -> Dict:
"""Convenience method: collect, save, and return today's news."""
Import
from MindSpider.BroadTopicExtraction.get_today_news import NewsCollector
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| sources | Optional[List[str]] | No | News source IDs (None = all 12) |
Outputs
| Name | Type | Description |
|---|---|---|
| news_list | List[Dict] | Collected news items |
| total_news | int | Total items collected |
| saved_count | int | Items saved to database |
Usage Examples
Collect Today's News
import asyncio
from MindSpider.BroadTopicExtraction.get_today_news import NewsCollector
async def main():
collector = NewsCollector()
result = await collector.collect_and_save_news()
print(f"Collected {result['total_news']} news from {len(result['successful_sources'])} sources")
for news in result['news_list'][:5]:
print(f" [{news.get('source_platform')}] {news.get('title')}")
asyncio.run(main())