Spaces:
Runtime error
Runtime error
Update scraper.py
Browse files- scraper.py +4 -3
scraper.py
CHANGED
@@ -2,11 +2,12 @@ import requests
|
|
2 |
from bs4 import BeautifulSoup
|
3 |
|
4 |
def scrape_url(url):
|
5 |
-
"""Fetch and extract text from a webpage."""
|
6 |
try:
|
7 |
-
res = requests.get(url, timeout=10)
|
8 |
res.raise_for_status()
|
9 |
soup = BeautifulSoup(res.text, 'html.parser')
|
10 |
-
|
|
|
|
|
11 |
except Exception as e:
|
12 |
return f"[Error scraping {url}: {e}]"
|
|
|
2 |
from bs4 import BeautifulSoup
|
3 |
|
4 |
def scrape_url(url):
|
|
|
5 |
try:
|
6 |
+
res = requests.get(url, timeout=10, headers={'User-Agent': 'Mozilla/5.0'})
|
7 |
res.raise_for_status()
|
8 |
soup = BeautifulSoup(res.text, 'html.parser')
|
9 |
+
for tag in soup(['script','style','']): tag.decompose()
|
10 |
+
text = soup.get_text(separator='\n', strip=True)
|
11 |
+
return text
|
12 |
except Exception as e:
|
13 |
return f"[Error scraping {url}: {e}]"
|