-
Notifications
You must be signed in to change notification settings - Fork 24
docs: unify crawl caps and fix the runnable code examples #1002
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 2 commits
Commits
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import json | ||
| from pathlib import Path | ||
| from tempfile import mkdtemp | ||
| from urllib.parse import urlsplit | ||
| from zipfile import ZipFile | ||
|
|
||
| from selenium import webdriver | ||
| from selenium.webdriver.chrome.options import Options as ChromeOptions | ||
|
|
||
| from apify import Actor | ||
|
|
||
|
|
||
| def proxy_auth_extension(proxy_url: str) -> str: | ||
| """Build a Chrome extension that routes Chrome through an authenticated proxy.""" | ||
| parts = urlsplit(proxy_url) | ||
|
|
||
| manifest = { | ||
| 'name': 'Apify Proxy', | ||
| 'version': '1.0.0', | ||
| 'manifest_version': 3, | ||
| 'permissions': ['proxy', 'webRequest', 'webRequestAuthProvider'], | ||
| 'host_permissions': ['<all_urls>'], | ||
| 'background': {'service_worker': 'background.js'}, | ||
| 'minimum_chrome_version': '108', | ||
| } | ||
|
|
||
| # The service worker sets the proxy and answers the auth challenge. | ||
| proxy_config = json.dumps( | ||
| { | ||
| 'mode': 'fixed_servers', | ||
| 'rules': { | ||
| 'singleProxy': { | ||
| 'scheme': parts.scheme, | ||
| 'host': parts.hostname, | ||
| 'port': parts.port, | ||
| }, | ||
| }, | ||
| } | ||
| ) | ||
| credentials = json.dumps( | ||
| {'username': parts.username or '', 'password': parts.password or ''} | ||
| ) | ||
| background = ( | ||
| 'chrome.proxy.settings.set(' | ||
| '{value: ' + proxy_config + ', scope: "regular"});\n' | ||
| 'chrome.webRequest.onAuthRequired.addListener(\n' | ||
| ' () => ({authCredentials: ' + credentials + '}),\n' | ||
| ' {urls: ["<all_urls>"]},\n' | ||
| ' ["blocking"],\n' | ||
| ');\n' | ||
| ) | ||
|
|
||
| extension_path = Path(mkdtemp()) / 'apify_proxy.zip' | ||
| with ZipFile(extension_path, 'w') as archive: | ||
| archive.writestr('manifest.json', json.dumps(manifest)) | ||
| archive.writestr('background.js', background) | ||
| return str(extension_path) | ||
|
|
||
|
|
||
| def build_chrome_driver(proxy_url: str) -> webdriver.Chrome: | ||
| """Create a headless Chrome WebDriver routed through an authenticated proxy.""" | ||
| chrome_options = ChromeOptions() | ||
|
|
||
| if Actor.configuration.headless: | ||
| # The new headless mode is required to load the proxy extension. | ||
| chrome_options.add_argument('--headless=new') | ||
|
|
||
| chrome_options.add_argument('--no-sandbox') | ||
| chrome_options.add_argument('--disable-dev-shm-usage') | ||
| chrome_options.add_argument('--disable-gpu') | ||
|
|
||
| # Load the proxy extension and keep it enabled in headless mode. | ||
| chrome_options.add_extension(proxy_auth_extension(proxy_url)) | ||
| chrome_options.add_argument( | ||
| '--disable-features=DisableLoadExtensionCommandLineSwitch' | ||
| ) | ||
|
|
||
| return webdriver.Chrome(options=chrome_options) |
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
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
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.