Spaces:
Build error
Build error
| from ddgs import DDGS | |
| import requests | |
| from bs4 import BeautifulSoup | |
| import time | |
| def search_web(query): | |
| try: | |
| with DDGS() as ddgs: | |
| results = list(ddgs.text(query, max_results=5)) | |
| return results | |
| except Exception as e: | |
| print(f"Search error: {e}") | |
| return [] | |
| def scrape_page(url): | |
| try: | |
| response = requests.get(url, timeout=5) | |
| soup = BeautifulSoup(response.text, "html.parser") | |
| paragraphs = soup.find_all("p") | |
| content = " ".join([p.get_text() for p in paragraphs[:20]]) | |
| return content | |
| except: | |
| return "" | |
| def gather_research(topic): | |
| time.sleep(2) | |
| results = search_web(topic) | |
| if not results: | |
| return "No search results found. Try again in a few minutes.", [] | |
| all_content = "" | |
| sources = [] | |
| for r in results: | |
| url = r.get("href", "") # back to href - confirmed correct | |
| title = r.get("title", url) | |
| if url: | |
| all_content += scrape_page(url) + "\n" | |
| sources.append(f"- [{title}]({url})") | |
| return all_content, sources |