-
Notifications
You must be signed in to change notification settings - Fork 148
Process request headers #38
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
+140
−21
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
dfcc3ff
Process request headers
elacuesta a46b1db
Typing fix
elacuesta c7225c4
Rename private request handler
elacuesta 7ed9d28
Additional typing fix
elacuesta b10bfb1
Clarifying comment
elacuesta 690d002
scrapy_playwright/handler.py: fix typo: reches → reaches
Gallaecio 874411e
Merge branch 'master' into process-request-headers
elacuesta 53e9259
Add test/docstring
elacuesta 16ac2a2
Docs for PLAYWRIGHT_PROCESS_REQUEST_HEADERS
elacuesta 42a1280
Update readme
elacuesta 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| from urllib.parse import urlparse | ||
|
|
||
| from playwright.async_api import Request as PlaywrightRequest | ||
| from scrapy.http.headers import Headers | ||
|
|
||
|
|
||
| """ | ||
| This module includes functions to process request headers. | ||
| Refer to the PLAYWRIGHT_PROCESS_REQUEST_HEADERS setting for more information. | ||
| """ | ||
|
|
||
|
|
||
| async def use_scrapy_headers( | ||
| browser_type: str, | ||
| playwright_request: PlaywrightRequest, | ||
| scrapy_headers: Headers, | ||
| ) -> dict: | ||
| """Scrapy headers take precedence over Playwright headers for navigation requests. | ||
| For non-navigation requests, only User-Agent is taken from the Scrapy headers.""" | ||
|
|
||
| headers = scrapy_headers.to_unicode_dict() | ||
|
|
||
| # Scrapy's user agent has priority over Playwright's | ||
| headers.setdefault("user-agent", playwright_request.headers.get("user-agent")) | ||
|
|
||
| if playwright_request.is_navigation_request(): | ||
| if browser_type == "firefox": | ||
| # otherwise this fails with playwright.helper.Error: NS_ERROR_NET_RESET | ||
| headers["host"] = urlparse(playwright_request.url).netloc | ||
| return headers | ||
| else: | ||
| # override user agent, for consistency with other requests | ||
| if headers.get("user-agent"): | ||
| playwright_request.headers["user-agent"] = headers["user-agent"] | ||
| return playwright_request.headers | ||
|
|
||
|
|
||
| async def use_playwright_headers( | ||
| browser_type: str, | ||
| playwright_request: PlaywrightRequest, | ||
| scrapy_headers: Headers, | ||
| ) -> dict: | ||
| """Return headers from the Playwright request, unaltered""" | ||
| return playwright_request.headers |
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.
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.
Shame, this could be a 1-liner in Scrapy 2.4+ 🙁
(technically also here, but I’m guessing you don’t want to have
load_objectparse a string in the default scenario)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.
Indeed, I'd like to avoid any (admittedly small) overhead if we already have the object.
This is a nice catch, I was planning on documenting the setting as accepting either paths or functions directly, but didn't remember that was only valid on Scrapy 2.4+. I'll be sure to mention that when I write the docs, thanks!