File size: 1,694 Bytes
840261a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import time
import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from bs4 import BeautifulSoup

class BrowserScraper:
    def __init__(self):
        self.options = Options()
        self.options.add_argument("--headless")
        self.options.add_argument("--no-sandbox")
        self.options.add_argument("--disable-dev-shm-usage")
        
        chrome_bin = os.getenv("CHROME_BIN")
        if chrome_bin:
            self.options.binary_location = chrome_bin

    def fetch_page_metadata(self, url):
        """
        Uses explicit Browser (Selenium) to scrape the page title and summary.
        This fulfills the requirement of 'real browser operation'.
        """
        driver = None
        try:
            print(f"🌍 Browser Navigating to: {url}")
            service = Service(ChromeDriverManager().install())
            driver = webdriver.Chrome(service=service, options=self.options)
            
            driver.get(url)
            time.sleep(2) # Wait for JS to load
            
            title = driver.title
            content = driver.find_element("tag name", "body").text[:500] # Get first 500 chars for summary
            
            return {
                "title": title,
                "summary": content,
                "status": "success"
            }
        except Exception as e:
            return {
                "title": "Error",
                "summary": str(e),
                "status": "failed"
            }
        finally:
            if driver:
                driver.quit()