1
- """
1
+ """
2
2
Module for making the request on the web
3
3
"""
4
4
import re
5
5
from typing import List
6
6
from langchain_community .tools import DuckDuckGoSearchResults
7
- from googlesearch import search
7
+ from googlesearch import search as google_search
8
+ from yahoo_search import search as yahoo_search
8
9
9
10
10
11
def search_on_web (query : str , search_engine : str = "Google" , max_results : int = 10 ) -> List [str ]:
@@ -29,18 +30,29 @@ def search_on_web(query: str, search_engine: str = "Google", max_results: int =
29
30
This function allows switching between Google and DuckDuckGo to perform internet searches, returning a list of result URLs.
30
31
"""
31
32
32
- if search_engine == "Google " :
33
+ if search_engine . lower () == "google " :
33
34
res = []
34
35
35
- for url in search (query , stop = max_results ):
36
+ for url in google_search (query , stop = max_results ):
36
37
res .append (url )
37
38
return res
38
- elif search_engine == "DuckDuckGo " :
39
+ elif search_engine . lower () == "duckduckgo " :
39
40
research = DuckDuckGoSearchResults (max_results = max_results )
40
41
res = research .run (query )
41
42
42
43
links = re .findall (r'https?://[^\s,\]]+' , res )
43
44
44
45
return links
46
+ elif search_engine .lower () == "yahoo" :
47
+ list_result = yahoo_search (query )
48
+ results = []
49
+ for page in list_result .pages :
50
+ if len (results ) >= max_results : # Check if max_results has already been reached
51
+ break # Exit loop if max_results has been reached
52
+ try :
53
+ results .append (page .link )
54
+ except AttributeError :
55
+ continue
56
+ return results
45
57
raise ValueError (
46
- "The only search engines avaiable are DuckDuckGo or Google" )
58
+ "The only search engines available are DuckDuckGo or Google" )
0 commit comments