feat: undected_chromedriver support

This commit is contained in:
ekinsenler 2024-10-06 22:50:52 +03:00
parent ea9ed1a981
commit 80ece2179a
2 changed files with 25 additions and 2 deletions

View File

@ -21,7 +21,8 @@ graph_config = {
"api_key": groq_key, "api_key": groq_key,
"temperature": 0 "temperature": 0
}, },
"headless": False "headless": False,
"backend": "undetected_chromedriver"
} }
# ************************************************ # ************************************************

View File

@ -61,6 +61,28 @@ class ChromiumLoader(BaseLoader):
self.urls = urls self.urls = urls
self.load_state = load_state self.load_state = load_state
async def ascrape_undetected_chromedriver(self, url: str) -> str:
"""
Asynchronously scrape the content of a given URL using undetected chrome with Selenium.
Args:
url (str): The URL to scrape.
Returns:
str: The scraped HTML content or an error message if an exception occurs.
"""
import undetected_chromedriver as uc
logger.info(f"Starting scraping with {self.backend}...")
results = ""
try:
driver = uc.Chrome()
results = driver.get(url).page_content
except Exception as e:
results = f"Error: {e}"
return results
async def ascrape_playwright(self, url: str) -> str: async def ascrape_playwright(self, url: str) -> str:
""" """
Asynchronously scrape the content of a given URL using Playwright's async API. Asynchronously scrape the content of a given URL using Playwright's async API.
@ -75,7 +97,7 @@ class ChromiumLoader(BaseLoader):
from playwright.async_api import async_playwright from playwright.async_api import async_playwright
from undetected_playwright import Malenia from undetected_playwright import Malenia
logger.info("Starting scraping...") logger.info(f"Starting scraping with {self.backend}...")
results = "" results = ""
async with async_playwright() as p: async with async_playwright() as p:
browser = await p.chromium.launch( browser = await p.chromium.launch(