Spaces:
Running
Running
File size: 1,929 Bytes
bce428a 2a0bf8d 2d64614 530d27a 2a0bf8d 2d64614 2a0bf8d 2e18161 2a0bf8d 530d27a 2a0bf8d 6a10786 2a0bf8d 2d64614 530d27a 2d64614 530d27a |
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 |
from selenium import webdriver
from selenium.common.exceptions import WebDriverException
from selenium import webdriver # type: ignore
from selenium.webdriver.chrome.service import Service # type: ignore
from selenium.webdriver.chrome.options import Options # type: ignore
from bs4 import BeautifulSoup # type: ignore
import time
# Define the ChromeDriver path directly
CHROME_DRIVER_PATH = "./chrome"
def scrape_website(website):
print("Connecting to Chrome Browser...")
# Setup ChromeDriver service and options
service = Service(CHROME_DRIVER_PATH)
options = Options()
options.add_argument("--headless") # Run in headless mode for deployment
driver = webdriver.Chrome(service=service, options=options)
try:
driver.get(website)
print("Waiting for CAPTCHA to be solved manually (if present)...")
# Optional waiting loop for manual CAPTCHA solving
while "captcha" in driver.page_source.lower():
print("CAPTCHA detected, waiting...")
time.sleep(5)
print("CAPTCHA solved or not present. Scraping page content...")
html = driver.page_source
return html
finally:
driver.quit()
def extract_body_content(html_content):
soup = BeautifulSoup(html_content, "html.parser")
body_content = soup.body
return str(body_content) if body_content else ""
def clean_body_content(body_content):
soup = BeautifulSoup(body_content, "html.parser")
for script_or_style in soup(["script", "style"]):
script_or_style.extract()
cleaned_content = soup.get_text(separator="\n")
cleaned_content = "\n".join(
line.strip() for line in cleaned_content.splitlines() if line.strip()
)
return cleaned_content
def split_dom_content(dom_content, max_length=6000):
return [dom_content[i:i + max_length] for i in range(0, len(dom_content), max_length)]
|