| |
| 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}]" |
|
|