Spaces:
Sleeping
Sleeping
Download tools/web_scraper.py from Lofty-Brambles/tds-project-2: direct link, hf CLI and curl.
- Browser
- Download file 1.16 kB
-
https://huggingface.co/spaces/Lofty-Brambles/tds-project-2/resolve/main/tools/web_scraper.py
- Command line
-
hf download hf://spaces/Lofty-Brambles/tds-project-2/tools/web_scraper.py
-
curl -L -o web_scraper.py https://huggingface.co/spaces/Lofty-Brambles/tds-project-2/resolve/main/tools/web_scraper.py
1.16 kB
| from langchain_core.tools import tool | |
| from playwright.sync_api import sync_playwright | |
| from bs4 import BeautifulSoup | |
| from urllib.parse import urljoin | |
| def get_rendered_html(url: str) -> dict: | |
| """ | |
| Fetch and return the fully rendered HTML of a webpage. | |
| """ | |
| print("\nFetching and rendering:", url) | |
| try: | |
| with sync_playwright() as p: | |
| browser = p.chromium.launch(headless=True) | |
| page = browser.new_page() | |
| page.goto(url, wait_until="networkidle") | |
| content = page.content() | |
| browser.close() | |
| # Parse images | |
| soup = BeautifulSoup(content, "html.parser") | |
| imgs = [urljoin(url, img["src"]) for img in soup.find_all("img", src=True)] | |
| if len(content) > 300000: | |
| print("Warning: HTML too large, truncating...") | |
| content = content[:300000] + "... [TRUNCATED DUE TO SIZE]" | |
| return { | |
| "html": content, | |
| "images": imgs, | |
| "url": url | |
| } | |
| except Exception as e: | |
| return {"error": f"Error fetching/rendering page: {str(e)}"} | |