forked from The-Pocket/PocketFlow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch_web.py
More file actions
38 lines (29 loc) · 1.09 KB
/
search_web.py
File metadata and controls
38 lines (29 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from duckduckgo_search import DDGS
def search_web(query, max_results=3):
"""
Search the web using DuckDuckGo.
Args:
query (str): The search query
max_results (int): Maximum number of results to return
Returns:
str: Formatted search results
"""
try:
ddgs = DDGS()
results = ddgs.text(query, max_results=max_results)
if not results:
return "No search results found."
formatted_results = []
for i, result in enumerate(results, 1):
title = result.get('title', 'No title')
body = result.get('body', 'No description')
href = result.get('href', 'No URL')
formatted_results.append(f"{i}. {title}\n{body}\nURL: {href}\n")
return "\n".join(formatted_results)
except Exception as e:
return f"Error during search: {str(e)}"
if __name__ == "__main__":
# Test the function
test_query = "Python programming"
print(f"Search Query: {test_query}")
print(f"Results:\n{search_web(test_query)}")