-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
### Description <!-- The purpose of the PR, list of the changes, ... --> Add a new decorator for processing pre navigation hooks Example Use: ```python from crawlee.playwright_crawler import PlaywrightCrawler from .routes import router async def main() -> None: """The crawler entry point.""" crawler = PlaywrightCrawler( request_handler=router, max_requests_per_crawl=50, ) @crawler.pre_navigation_hook async def hooky(context) -> None: print(f'Hook1') @crawler.pre_navigation_hook async def hooky2(context) -> None: print(f'Hook2') await crawler.run( [ 'https://crawlee.dev', ] ) ``` ### Issues <!-- If applicable, reference any related GitHub issues --> - Closes: #427 ### Checklist - [x] CI passed
- Loading branch information
1 parent
f9463e7
commit 5dd5b60
Showing
7 changed files
with
97 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
try: | ||
from ._playwright_crawler import PlaywrightCrawler | ||
from ._playwright_crawling_context import PlaywrightCrawlingContext | ||
from ._playwright_pre_navigation_context import PlaywrightPreNavigationContext | ||
except ImportError as exc: | ||
raise ImportError( | ||
"To import anything from this subpackage, you need to install the 'playwright' extra." | ||
"For example, if you use pip, run `pip install 'crawlee[playwright]'`.", | ||
) from exc | ||
|
||
__all__ = ['PlaywrightCrawler', 'PlaywrightCrawlingContext'] | ||
__all__ = ['PlaywrightCrawler', 'PlaywrightCrawlingContext', 'PlaywrightPreNavigationContext'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/crawlee/playwright_crawler/_playwright_pre_navigation_context.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
from typing import TYPE_CHECKING | ||
|
||
from crawlee._types import BasicCrawlingContext | ||
|
||
if TYPE_CHECKING: | ||
from playwright.async_api import Page | ||
|
||
|
||
@dataclass(frozen=True) | ||
class PlaywrightPreNavigationContext(BasicCrawlingContext): | ||
"""Context used by PlaywrightCrawler. | ||
It Provides access to the `Page` object for the current browser page. | ||
""" | ||
|
||
page: Page | ||
"""The Playwright `Page` object for the current page.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters