EvoConvo / web_search.py
HemanM's picture
Update web_search.py
d016a6e verified
raw
history blame contribute delete
711 Bytes
# 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}]"