-
Notifications
You must be signed in to change notification settings - Fork 139
Multiple browser contexts #13
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
26 commits
Select commit
Hold shift + click to select a range
4f9d57b
Multiple browser contexts
elacuesta ca963ba
Small changes
elacuesta dc28703
Update readme and contexts example
elacuesta 7f93b6b
Merge branch 'master' into multiple-contexts
elacuesta b817521
Close context during crawl
elacuesta 8d24680
test browser context defined at startup
elacuesta f1f2736
add missing import
elacuesta d440a15
Close browser context callback
elacuesta ecc24f0
Test dynamic context creation
elacuesta 9ea0d51
Small test restructure
elacuesta c60cbcd
Use asyncio.gather to create contexts, reorganize methods
elacuesta 9d9efa0
Merge remote-tracking branch 'origin/master' into multiple-contexts
elacuesta 1b0c0b0
Deprecate PLAYWRIGHT_CONTEXT_ARGS setting
elacuesta 3ef717d
Update deprecation message for PLAYWRIGHT_CONTEXT_ARGS
elacuesta cea8439
Readme: add usage docs for multiple contexts
elacuesta 28780e9
Update readme link
elacuesta eb73318
More readme links
elacuesta 560a63e
Merge remote-tracking branch 'origin/master' into multiple-contexts
elacuesta 4344d82
Merge branch 'master' into multiple-contexts
elacuesta 4b223bd
Update readme examples
elacuesta e3a5300
Simplify context creation
elacuesta 04cae8d
Only use PLAYWRIGHT_CONTEXT_ARGS as default kwargs for the default co…
elacuesta 3a9a001
Merge branch 'master' into multiple-contexts
elacuesta defa0a8
Logging level for context creation
elacuesta 1011243
Improve deprecation message for PLAYWRIGHT_CONTEXT_ARGS
elacuesta 9c4c687
Update readme with note about existing contexts
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,107 @@ | ||
from scrapy import Spider, Request | ||
from scrapy.crawler import CrawlerProcess | ||
|
||
|
||
class MultipleContextsSpider(Spider): | ||
"""Handle multiple browser contexts""" | ||
|
||
name = "contexts" | ||
custom_settings = { | ||
"PLAYWRIGHT_CONTEXTS": { | ||
"first": { | ||
"storage_state": { | ||
"cookies": [ | ||
{ | ||
"url": "https://httpbin.org/headers", | ||
"name": "context", | ||
"value": "first", | ||
}, | ||
], | ||
}, | ||
}, | ||
"second": { | ||
"storage_state": { | ||
"cookies": [ | ||
{ | ||
"url": "https://httpbin.org/headers", | ||
"name": "context", | ||
"value": "second", | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
def start_requests(self): | ||
# using existing contexts | ||
yield Request( | ||
url="https://httpbin.org/headers", | ||
meta={ | ||
"playwright": True, | ||
"playwright_context": "first", | ||
"playwright_include_page": True, | ||
}, | ||
dont_filter=True, | ||
) | ||
yield Request( | ||
url="https://httpbin.org/headers", | ||
meta={ | ||
"playwright": True, | ||
"playwright_context": "second", | ||
"playwright_include_page": True, | ||
}, | ||
dont_filter=True, | ||
) | ||
# create a new context | ||
yield Request( | ||
url="https://httpbin.org/headers", | ||
meta={ | ||
"playwright": True, | ||
"playwright_context": "third", | ||
"playwright_context_kwargs": { | ||
"storage_state": { | ||
"cookies": [ | ||
{ | ||
"url": "https://httpbin.org/headers", | ||
"name": "context", | ||
"value": "third", | ||
}, | ||
], | ||
}, | ||
}, | ||
"playwright_include_page": True, | ||
}, | ||
dont_filter=True, | ||
) | ||
# default context | ||
yield Request( | ||
url="https://httpbin.org/headers", | ||
meta={"playwright": True, "playwright_include_page": True}, | ||
dont_filter=True, | ||
) | ||
|
||
async def parse(self, response): | ||
page = response.meta["playwright_page"] | ||
context_name = response.meta["playwright_context"] | ||
storage_state = await page.context.storage_state() | ||
await page.context.close() | ||
return { | ||
"url": response.url, | ||
"context": context_name, | ||
"cookies": storage_state["cookies"], | ||
} | ||
|
||
|
||
if __name__ == "__main__": | ||
process = CrawlerProcess( | ||
settings={ | ||
"TWISTED_REACTOR": "twisted.internet.asyncioreactor.AsyncioSelectorReactor", | ||
"DOWNLOAD_HANDLERS": { | ||
"https": "scrapy_playwright.handler.ScrapyPlaywrightDownloadHandler", | ||
# "http": "scrapy_playwright.handler.ScrapyPlaywrightDownloadHandler", | ||
}, | ||
} | ||
) | ||
process.crawl(MultipleContextsSpider) | ||
process.start() |
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.