-
Notifications
You must be signed in to change notification settings - Fork 741
feat(cli): Add Adaptive and Stagehand crawler templates #1888
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
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cc2a712
feat: Add HTTP and Adaptive crawler templates
vdusek b11f51d
rm HTTP crawler
vdusek 464fc58
Add stagehand
vdusek 6986eef
Simplification
vdusek 5f331df
feat: Read Stagehand OpenAI key from OPENAI_API_KEY env var in template
vdusek 9e706c4
feat: Use ValueError for missing OPENAI_API_KEY in Stagehand template
vdusek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
12 changes: 12 additions & 0 deletions
12
src/crawlee/project_template/templates/main_adaptive_beautifulsoup.py
This file contains hidden or 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,12 @@ | ||
| # % extends 'main.py' | ||
|
|
||
| # % block import | ||
| from crawlee.crawlers import AdaptivePlaywrightCrawler | ||
| # % endblock | ||
|
|
||
| # % block instantiation | ||
| crawler = AdaptivePlaywrightCrawler.with_beautifulsoup_static_parser( | ||
| request_handler=router, | ||
| max_requests_per_crawl=10, | ||
| {{ self.http_client_instantiation() }}) | ||
| # % endblock |
12 changes: 12 additions & 0 deletions
12
src/crawlee/project_template/templates/main_adaptive_parsel.py
This file contains hidden or 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,12 @@ | ||
| # % extends 'main.py' | ||
|
|
||
| # % block import | ||
| from crawlee.crawlers import AdaptivePlaywrightCrawler | ||
| # % endblock | ||
|
|
||
| # % block instantiation | ||
| crawler = AdaptivePlaywrightCrawler.with_parsel_static_parser( | ||
| request_handler=router, | ||
| max_requests_per_crawl=10, | ||
| {{ self.http_client_instantiation() }}) | ||
| # % endblock |
This file contains hidden or 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,17 @@ | ||
| # % extends 'main.py' | ||
|
|
||
| # % block import | ||
| from crawlee.browsers import StagehandOptions | ||
| from crawlee.crawlers import StagehandCrawler | ||
| # % endblock | ||
|
|
||
| # % block instantiation | ||
| crawler = StagehandCrawler( | ||
| request_handler=router, | ||
| headless=True, | ||
| max_requests_per_crawl=10, | ||
| stagehand_options=StagehandOptions( | ||
| model_api_key='<YOUR_OPENAI_API_KEY>', | ||
| ), | ||
| {{ self.http_client_instantiation() }}) | ||
| # % endblock | ||
19 changes: 19 additions & 0 deletions
19
src/crawlee/project_template/templates/routes_adaptive_beautifulsoup.py
This file contains hidden or 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,19 @@ | ||
| from crawlee.crawlers import AdaptivePlaywrightCrawlingContext | ||
| from crawlee.router import Router | ||
|
|
||
| router = Router[AdaptivePlaywrightCrawlingContext]() | ||
|
|
||
|
|
||
| @router.default_handler | ||
| async def default_handler(context: AdaptivePlaywrightCrawlingContext) -> None: | ||
| """Default request handler.""" | ||
| context.log.info(f'Processing {context.request.url} ...') | ||
| title = context.parsed_content.find('title') | ||
| await context.push_data( | ||
| { | ||
| 'url': context.request.loaded_url, | ||
| 'title': title.text if title else None, | ||
| } | ||
| ) | ||
|
|
||
| await context.enqueue_links() |
19 changes: 19 additions & 0 deletions
19
src/crawlee/project_template/templates/routes_adaptive_parsel.py
This file contains hidden or 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,19 @@ | ||
| from crawlee.crawlers import AdaptivePlaywrightCrawlingContext | ||
| from crawlee.router import Router | ||
|
|
||
| router = Router[AdaptivePlaywrightCrawlingContext]() | ||
|
|
||
|
|
||
| @router.default_handler | ||
| async def default_handler(context: AdaptivePlaywrightCrawlingContext) -> None: | ||
| """Default request handler.""" | ||
| context.log.info(f'Processing {context.request.url} ...') | ||
| title = context.parsed_content.xpath('//title/text()').get() | ||
| await context.push_data( | ||
| { | ||
| 'url': context.request.loaded_url, | ||
| 'title': title, | ||
| } | ||
| ) | ||
|
|
||
| await context.enqueue_links() |
21 changes: 21 additions & 0 deletions
21
src/crawlee/project_template/templates/routes_stagehand.py
This file contains hidden or 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,21 @@ | ||
| from crawlee.crawlers import StagehandCrawlingContext | ||
| from crawlee.router import Router | ||
|
|
||
| router = Router[StagehandCrawlingContext]() | ||
|
|
||
|
|
||
| @router.default_handler | ||
| async def default_handler(context: StagehandCrawlingContext) -> None: | ||
| """Default request handler.""" | ||
| context.log.info(f'Processing {context.request.url} ...') | ||
|
|
||
| data = await context.page.extract(instruction='Get the page title and main heading.') | ||
|
|
||
| await context.push_data( | ||
| { | ||
| 'url': context.request.loaded_url, | ||
| 'data': data.model_dump(), | ||
| } | ||
| ) | ||
|
|
||
| await context.enqueue_links() |
15 changes: 8 additions & 7 deletions
15
src/crawlee/project_template/{{cookiecutter.project_name}}/Dockerfile
This file contains hidden or 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
12 changes: 7 additions & 5 deletions
12
src/crawlee/project_template/{{cookiecutter.project_name}}/pyproject.toml
This file contains hidden or 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
13 changes: 7 additions & 6 deletions
13
src/crawlee/project_template/{{cookiecutter.project_name}}/requirements.txt
This file contains hidden or 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,18 +1,19 @@ | ||
| # % if cookiecutter.crawler_type == 'playwright-camoufox' | ||
| camoufox[geoip]~=0.4.5 | ||
| # % endif | ||
| # % if cookiecutter.crawler_type.startswith('playwright') | ||
| # % if cookiecutter.crawler_type.startswith('adaptive-') | ||
| # % set extras = ['adaptive-crawler', 'beautifulsoup', 'parsel'] | ||
| # % elif cookiecutter.crawler_type.startswith('playwright') | ||
| # % set extras = ['playwright'] | ||
| # % elif cookiecutter.crawler_type == 'stagehand' | ||
| # % set extras = ['stagehand'] | ||
| # % else | ||
| # % set extras = [cookiecutter.crawler_type] | ||
| # % endif | ||
| # % if cookiecutter.enable_apify_integration | ||
| apify | ||
| # % endif | ||
| # % if cookiecutter.http_client == 'curl-impersonate' | ||
| # % do extras.append('curl-impersonate') | ||
| # % endif | ||
| # % if cookiecutter.http_client == 'httpx' | ||
| # % do extras.append('httpx') | ||
| # % if cookiecutter.http_client in ('httpx', 'curl-impersonate') | ||
| # % do extras.append(cookiecutter.http_client) | ||
| # % endif | ||
| crawlee[{{ extras | join(',') }}] |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.