-
Notifications
You must be signed in to change notification settings - Fork 395
feat: Add requests
argument to EnqueueLinksFunction
#1024
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 all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
91b6ad6
Add `requests` argument to `EnqueueLinksFunction`
Pijukatel 4884523
Update docstring
Pijukatel d18feb1
Add overloads and tests
Pijukatel 44b0a45
Merge remote-tracking branch 'origin/master' into enqueueueu-links
Pijukatel 21919a1
Merge remote-tracking branch 'origin/master' into enqueueueu-links
Pijukatel dfa892c
Merge remote-tracking branch 'origin/master' into enqueueueu-links
Pijukatel 8c471dc
Add ExtractLinks protocol and use it in contexts
Pijukatel c8f84ad
Improve docs. Add examples.
Pijukatel 896cb08
Expand ExtractLinksFunction doc string
Pijukatel 707cd38
Merge remote-tracking branch 'origin/master' into enqueueueu-links
Pijukatel d12185f
Use new docs style with Run button
Pijukatel 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
36 changes: 36 additions & 0 deletions
36
docs/examples/code_examples/extract_and_add_specific_links_on_website_bs.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,36 @@ | ||
import asyncio | ||
|
||
from crawlee import Glob | ||
from crawlee.crawlers import BeautifulSoupCrawler, BeautifulSoupCrawlingContext | ||
|
||
|
||
async def main() -> None: | ||
crawler = BeautifulSoupCrawler( | ||
# Limit the crawl to max requests. Remove or increase it for crawling all links. | ||
max_requests_per_crawl=10, | ||
) | ||
|
||
# Define the default request handler, which will be called for every request. | ||
@crawler.router.default_handler | ||
async def request_handler(context: BeautifulSoupCrawlingContext) -> None: | ||
context.log.info(f'Processing {context.request.url} ...') | ||
|
||
# Extract all the documentation links found on the page, except for the examples. | ||
extracted_links = await context.extract_links( | ||
include=[Glob('https://crawlee.dev/docs/**')], | ||
exclude=[Glob('https://crawlee.dev/docs/examples')], | ||
) | ||
# Some very custom filtering which can't be achieved by `extract_links` arguments. | ||
max_link_length = 30 | ||
filtered_links = [ | ||
link for link in extracted_links if len(link.url) < max_link_length | ||
] | ||
# Add filtered links to the request queue. | ||
await context.add_requests(filtered_links) | ||
|
||
# Run the crawler with the initial list of requests. | ||
await crawler.run(['https://crawlee.dev']) | ||
|
||
|
||
if __name__ == '__main__': | ||
asyncio.run(main()) |
36 changes: 36 additions & 0 deletions
36
docs/examples/code_examples/extract_and_add_specific_links_on_website_pw.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,36 @@ | ||
import asyncio | ||
|
||
from crawlee import Glob | ||
from crawlee.crawlers import PlaywrightCrawler, PlaywrightCrawlingContext | ||
|
||
|
||
async def main() -> None: | ||
crawler = PlaywrightCrawler( | ||
# Limit the crawl to max requests. Remove or increase it for crawling all links. | ||
max_requests_per_crawl=10, | ||
) | ||
|
||
# Define the default request handler, which will be called for every request. | ||
@crawler.router.default_handler | ||
async def request_handler(context: PlaywrightCrawlingContext) -> None: | ||
context.log.info(f'Processing {context.request.url} ...') | ||
|
||
# Extract all the documentation links found on the page, except for the examples. | ||
extracted_links = await context.extract_links( | ||
include=[Glob('https://crawlee.dev/docs/**')], | ||
exclude=[Glob('https://crawlee.dev/docs/examples')], | ||
) | ||
# Some very custom filtering which can't be achieved by `extract_links` arguments. | ||
max_link_length = 30 | ||
filtered_links = [ | ||
link for link in extracted_links if len(link.url) < max_link_length | ||
] | ||
# Add filtered links to the request queue. | ||
await context.add_requests(filtered_links) | ||
|
||
# Run the crawler with the initial list of requests. | ||
await crawler.run(['https://crawlee.dev']) | ||
|
||
|
||
if __name__ == '__main__': | ||
asyncio.run(main()) |
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
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I now noticed that the JS counterpart accepts just
urls
as an array of strings. We should either restrict this, or extend the JS version 🙂If we choose restricting this one, then most of the other parameters (barring
selector
) would actually start making sense in combination withurls
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer to keep it as it is for consistency, since we use
request: str | Request
everywhere else.