Spaces:
Build error
Build error
| import requests | |
| import time | |
| from bs4 import BeautifulSoup | |
| from ddgs import DDGS | |
| SKIP_DOMAINS = [ | |
| "tripadvisor", "tiktok", "facebook", "instagram", | |
| "twitter", "yelp", "google", "wikipedia", "youtube", | |
| "infoguidenigeria", "nairaland" | |
| ] | |
| def search_leads(query, max_results=10): | |
| try: | |
| time.sleep(2) | |
| with DDGS() as ddgs: | |
| results = list(ddgs.text(query, max_results=max_results)) | |
| return results | |
| except Exception as e: | |
| print(f"Search error: {e}") | |
| return [] | |
| def scrape_website(url): | |
| try: | |
| response = requests.get(url, timeout=5) | |
| response.raise_for_status() | |
| soup = BeautifulSoup(response.text, "html.parser") | |
| paragraphs = soup.find_all("p") | |
| content = " ".join([p.get_text() for p in paragraphs[:15]]) | |
| return content[:800] | |
| except Exception: | |
| return "" | |
| def gather_leads(target_audience): | |
| results = search_leads(target_audience) | |
| leads = [] | |
| for r in results: | |
| url = r.get("href", "") | |
| title = r.get("title", "") | |
| body = r.get("body", "") | |
| # skip directories and social media | |
| if url and any(domain in url.lower() for domain in SKIP_DOMAINS): | |
| continue | |
| if url: | |
| extra_info = scrape_website(url) | |
| leads.append({ | |
| "name": title, | |
| "website": url, | |
| "summary": body, | |
| "details": extra_info | |
| }) | |
| return leads | |