Skip to content

Refactoring of pr #729 #730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions examples/extras/undected_playwrigth.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undetected_selenium should be a better name for this one, as it uses a selenium backend

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi @ekinsenler I would prefer to use this name because I think it could create confusion

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""
Basic example of scraping pipeline using SmartScraper
"""

import os
from dotenv import load_dotenv
from scrapegraphai.graphs import SmartScraperGraph
from scrapegraphai.utils import prettify_exec_info

load_dotenv()

# ************************************************
# Define the configuration for the graph
# ************************************************

groq_key = os.getenv("GROQ_APIKEY")

graph_config = {
"llm": {
"model": "groq/gemma-7b-it",
"api_key": groq_key,
"temperature": 0
},
"headless": False,
"backend": "undetected_chromedriver"
}

# ************************************************
# Create the SmartScraperGraph instance and run it
# ************************************************

smart_scraper_graph = SmartScraperGraph(
prompt="List me all the projects with their description.",
# also accepts a string with the already downloaded HTML code
source="https://perinim.github.io/projects/",
config=graph_config
)

result = smart_scraper_graph.run()
print(result)

# ************************************************
# Get graph execution info
# ************************************************

graph_exec_info = smart_scraper_graph.get_execution_info()
print(prettify_exec_info(graph_exec_info))
2 changes: 1 addition & 1 deletion examples/groq/smart_scraper_groq.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"api_key": groq_key,
"temperature": 0
},
"headless": False
"headless": False,
}

# ************************************************
Expand Down
24 changes: 23 additions & 1 deletion scrapegraphai/docloaders/chromium.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,28 @@ def __init__(
self.urls = urls
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(headless=self.headless)
results = driver.get(url).page_content
except Exception as e:
results = f"Error: {e}"
return results

async def ascrape_playwright(self, url: str) -> str:
"""
Asynchronously scrape the content of a given URL using Playwright's async API.
Expand All @@ -75,7 +97,7 @@ async def ascrape_playwright(self, url: str) -> str:
from playwright.async_api import async_playwright
from undetected_playwright import Malenia

logger.info("Starting scraping...")
logger.info(f"Starting scraping with {self.backend}...")
results = ""
async with async_playwright() as p:
browser = await p.chromium.launch(
Expand Down