File size: 2,591 Bytes
d08010b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin, urlparse


def scrape_url(url: str) -> dict:
    headers = {
        "User-Agent": (
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
            "AppleWebKit/537.36 (KHTML, like Gecko) "
            "Chrome/120.0.0.0 Safari/537.36"
        ),
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
        "Accept-Language": "en-US,en;q=0.5",
        "Accept-Encoding": "gzip, deflate",
        "Connection": "keep-alive",
    }

    resp = requests.get(url, headers=headers, timeout=15)
    resp.raise_for_status()
    soup = BeautifulSoup(resp.text, "html.parser")

    # ── Title ─────────────────────────────────────────────
    title = ""
    if soup.find("h1"):
        title = soup.find("h1").get_text(strip=True)
    elif soup.title:
        title = soup.title.get_text(strip=True)

    # ── Text ──────────────────────────────────────────────
    # Remove nav, footer, script, style tags first
    for tag in soup(["script", "style", "nav", "footer", "header", "aside"]):
        tag.decompose()

    paragraphs = [
        p.get_text(strip=True)
        for p in soup.find_all("p")
        if len(p.get_text(strip=True)) > 40   # skip short/nav paragraphs
    ]
    text = " ".join(paragraphs)

    if not text or len(text) < 100:
        # Fallback β€” grab all visible text
        text = soup.get_text(separator=" ", strip=True)

    # Limit to 10K words
    text = " ".join(text.split()[:10000])

    # ── Images ────────────────────────────────────────────
    imgs = []
    for img in soup.find_all("img"):
        if len(imgs) >= 5:
            break
        src = img.get("src") or img.get("data-src") or ""
        if not src or src.startswith("data:"):
            continue

        # Resolve relative URLs
        src = urljoin(url, src)
        if not src.startswith("http"):
            continue

        # Skip tiny icons
        try:
            w = int(img.get("width", 0))
            h = int(img.get("height", 0))
            if w and h and (w < 100 or h < 100):
                continue
        except (ValueError, TypeError):
            pass

        imgs.append(src)

    return {
        "title":  title,
        "text":   text,
        "images": imgs
    }