Spaces:
Sleeping
Sleeping
| from duckduckgo_search import DDGS | |
| # 可信來源清單,可以依需求擴充 | |
| TRUSTED_SITES = [ | |
| "site:gov.tw", | |
| "site:publicholidays.tw", | |
| "site:timeanddate.com" | |
| ] | |
| def trusted_web_search(query, max_results=5): | |
| """ | |
| 從可信任網站搜尋資料 | |
| """ | |
| try: | |
| ddg = DDGS() | |
| query_with_sources = f"{query} ({' OR '.join(TRUSTED_SITES)})" | |
| results = ddg.text(query_with_sources, max_results=max_results) | |
| if not results: | |
| return "找不到相關資訊" | |
| formatted = [] | |
| for r in results: | |
| formatted.append(f"{r['title']} - {r['body']} ({r['href']})") | |
| return "\n".join(formatted) | |
| except Exception as e: | |
| return f"搜尋失敗:{e}" | |