File size: 711 Bytes
d016a6e
52a306d
 
 
d016a6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# web_search.py
import requests

def web_search(query, max_results=3):
    try:
        url = f"https://duckduckgo.com/html/?q={query}"
        headers = {'User-Agent': 'Mozilla/5.0'}
        response = requests.get(url, headers=headers)
        results = []
        for line in response.text.split("\n"):
            if "<a rel=\"nofollow\"" in line and 'href="' in line:
                start = line.find('href="') + 6
                end = line.find('"', start)
                link = line[start:end]
                results.append(link)
            if len(results) >= max_results:
                break
        return "\n".join(results)
    except Exception as e:
        return f"[Web search failed: {e}]"