Skip to content

Commit aa2160c

Browse files
committed
feat: add research with bing + test function
1 parent 073d226 commit aa2160c

File tree

2 files changed

+52
-10
lines changed

2 files changed

+52
-10
lines changed
Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,61 @@
11
"""
2-
Module for making the request on the web
2+
research web module
33
"""
44
import re
55
from typing import List
66
from langchain_community.tools import DuckDuckGoSearchResults
77
from googlesearch import search as google_search
8-
8+
import requests
9+
from bs4 import BeautifulSoup
910

1011
def search_on_web(query: str, search_engine: str = "Google", max_results: int = 10) -> List[str]:
1112
"""
1213
Searches the web for a given query using specified search engine options.
1314
1415
Args:
1516
query (str): The search query to find on the internet.
16-
search_engine (str, optional): Specifies the search engine to use, options include 'Google' or 'DuckDuckGo'. Default is 'Google'.
17+
search_engine (str, optional): Specifies the search engine to use, options include 'Google', 'DuckDuckGo', or 'Bing'. Default is 'Google'.
1718
max_results (int, optional): The maximum number of search results to return.
1819
1920
Returns:
2021
List[str]: A list of URLs as strings that are the search results.
2122
2223
Raises:
23-
ValueError: If the search engine specified is neither 'Google' nor 'DuckDuckGo'.
24+
ValueError: If the search engine specified is neither 'Google', 'DuckDuckGo', nor 'Bing'.
2425
2526
Example:
2627
>>> search_on_web("example query", search_engine="Google", max_results=5)
2728
['http://example.com', 'http://example.org', ...]
2829
29-
This function allows switching between Google and DuckDuckGo to perform
30+
This function allows switching between Google, DuckDuckGo, and Bing to perform
3031
internet searches, returning a list of result URLs.
3132
"""
3233

3334
if search_engine.lower() == "google":
3435
res = []
35-
3636
for url in google_search(query, stop=max_results):
3737
res.append(url)
3838
return res
39+
3940
elif search_engine.lower() == "duckduckgo":
4041
research = DuckDuckGoSearchResults(max_results=max_results)
4142
res = research.run(query)
42-
4343
links = re.findall(r'https?://[^\s,\]]+', res)
44-
4544
return links
46-
raise ValueError(
47-
"The only search engines available are DuckDuckGo or Google")
45+
46+
elif search_engine.lower() == "bing":
47+
headers = {
48+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
49+
}
50+
search_url = f"https://www.bing.com/search?q={query}"
51+
response = requests.get(search_url, headers=headers)
52+
response.raise_for_status()
53+
soup = BeautifulSoup(response.text, "html.parser")
54+
55+
search_results = []
56+
for result in soup.find_all('li', class_='b_algo', limit=max_results):
57+
link = result.find('a')['href']
58+
search_results.append(link)
59+
return search_results
60+
61+
raise ValueError("The only search engines available are DuckDuckGo, Google, or Bing")

tests/utils/research_web_test.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import pytest
2+
from scrapegraphai.utils.research_web import search_on_web # Replace with actual path to your file
3+
4+
5+
def test_google_search():
6+
"""Tests search_on_web with Google search engine."""
7+
results = search_on_web("test query", search_engine="Google", max_results=2)
8+
assert len(results) == 2
9+
# You can further assert if the results actually contain 'test query' in the title/snippet using additional libraries
10+
11+
def test_bing_search():
12+
"""Tests search_on_web with Bing search engine."""
13+
results = search_on_web("test query", search_engine="Bing", max_results=1)
14+
assert results is not None
15+
# You can further assert if the results contain '.com' or '.org' in the domain
16+
17+
18+
def test_invalid_search_engine():
19+
"""Tests search_on_web with invalid search engine."""
20+
with pytest.raises(ValueError):
21+
search_on_web("test query", search_engine="Yahoo", max_results=5)
22+
23+
24+
def test_max_results():
25+
"""Tests search_on_web with different max_results values."""
26+
results_5 = search_on_web("test query", max_results=5)
27+
results_10 = search_on_web("test query", max_results=10)
28+
assert len(results_5) <= len(results_10)

0 commit comments

Comments
 (0)