File size: 2,099 Bytes
a24ecbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import httpx
from bs4 import BeautifulSoup
from typing import Optional

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"
    )
}

class Website:
    def __init__(self, url: str):
        self.url = url
        self.title = ""
        self.text = ""
        self.links: list[str] = []
        self._fetch()
        
    def _fetch(self):
        try:
            response = httpx.get(self.url, headers=HEADERS, timeout=15, follow_redirects=True)
            response.raise_for_status()
        except httpx.HTTPError as e:
            raise ValueError(f"Impossible de charger la page {self.url} : {e}")
        
        soup = BeautifulSoup(response.content, "html.parser")
        
        self.title = soup.title.string.strip() if soup.title else "Sans titre"
        
        # Supprimer les elements non-textuels
        for tag in soup(["script", "style", "img", "input", "nav", "footer"]):
            tag.decompose()
            
        self.text = soup.get_text(separator="\n", strip=True) if soup.body else ""
        
        # Extraire les liens ansolus uniquement
        raw_links = [a.get("href") for a in soup.find_all("a") if a.get("href")]
        self.links = [
            link for link in raw_links
            if link.startswith("http") and len(link) < 200
        ]
        
    def get_contents(self) -> str:
        # Tronquer a 500 chars pour ne pas depasser le context window
        truncated = self.text[:5000]
        return f"**Titre:** {self.titre} \n\n**Contenu:**\n{truncated}" 

def scrape_multiple(urls: list[str]) -> str:
    """
    Scrape plusieurs URLs et agrege leur contenu.
    Utilise pour combiner page principale + sous-pages (about, careers...)
    """
    combined = ""
    for url in urls:
        try:
            site = Website(url)
            combined += f"\n\n---\n## Source: {url}\n{site.get_contents()}"
        except Exception as e:
            combined += f"\n\n## Source: {url}\n[Erreur: {e}]"
    return combined