Leonydis137 commited on
Commit
6e8b62c
·
verified ·
1 Parent(s): 9957774

Rename web_search.py to web_searcher.py

Browse files
Files changed (2) hide show
  1. web_search.py +0 -11
  2. web_searcher.py +11 -0
web_search.py DELETED
@@ -1,11 +0,0 @@
1
-
2
- import requests
3
- from bs4 import BeautifulSoup
4
-
5
- def web_search(query):
6
- url = f"https://html.duckduckgo.com/html?q={query}"
7
- headers = {"User-Agent": "Mozilla/5.0"}
8
- res = requests.get(url, headers=headers)
9
- soup = BeautifulSoup(res.text, "html.parser")
10
- results = soup.find_all("a", {"class": "result__a"}, limit=3)
11
- return "\n".join(link.text for link in results) if results else "No results found."
 
 
 
 
 
 
 
 
 
 
 
 
web_searcher.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from duckduckgo_search import DDGS
2
+ import json
3
+
4
+ class WebSearcher:
5
+ def search(self, query, max_results=5):
6
+ try:
7
+ with DDGS() as ddgs:
8
+ results = [r for r in ddgs.text(query, max_results=max_results)]
9
+ return json.dumps(results, indent=2)
10
+ except Exception as e:
11
+ return f"Search error: {str(e)}"