@@ -128,7 +128,8 @@ Type `dict`, default `{}`
128128
129129A dictionary with options to be passed as keyword arguments when launching the
130130Browser. See the docs for
131- [ ` BrowserType.launch ` ] ( https://playwright.dev/python/docs/api/class-browsertype#browser-type-launch ) .
131+ [ ` BrowserType.launch ` ] ( https://playwright.dev/python/docs/api/class-browsertype#browser-type-launch )
132+ for a list of supported keyword arguments.
132133
133134``` python
134135PLAYWRIGHT_LAUNCH_OPTIONS = {
@@ -524,7 +525,7 @@ class AwesomeSpiderWithPage(scrapy.Spider):
524525
525526Multiple [ browser contexts] ( https://playwright.dev/python/docs/browser-contexts )
526527to be launched at startup can be defined via the
527- [ ` PLAYWRIGHT_CONTEXTS ` ] ( #PLAYWRIGHT_CONTEXTS ) setting.
528+ [ ` PLAYWRIGHT_CONTEXTS ` ] ( #playwright_contexts ) setting.
528529
529530### Choosing a specific context for a request
530531
@@ -548,7 +549,12 @@ context can also be customized on startup via the `PLAYWRIGHT_CONTEXTS` setting.
548549Pass a value for the ` user_data_dir ` keyword argument to launch a context as
549550persistent. See also [ ` BrowserType.launch_persistent_context ` ] ( https://playwright.dev/python/docs/api/class-browsertype#browser-type-launch-persistent-context ) .
550551
551- ### Creating a context during a crawl
552+ Note that persistent contexts are launched independently from the main browser
553+ instance, hence keyword arguments passed in the
554+ [ ` PLAYWRIGHT_LAUNCH_OPTIONS ` ] ( #playwright_launch_options )
555+ setting do not apply.
556+
557+ ### Creating contexts while crawling
552558
553559If the context specified in the ` playwright_context ` meta key does not exist, it will be created.
554560You can specify keyword arguments to be passed to
@@ -583,7 +589,7 @@ Specifying a non-negative integer value for the
583589[ ` PLAYWRIGHT_CLOSE_CONTEXT_INTERVAL ` ] ( #playwright_close_context_interval )
584590setting enables closing browser contexts which have no active pages.
585591
586- ### Closing a context during a crawl
592+ ### Closing contexts while crawling
587593
588594After [ receiving the Page object in your callback] ( #receiving-page-objects-in-callbacks ) ,
589595you can access a context though the corresponding [ ` Page.context ` ] ( https://playwright.dev/python/docs/api/class-page#page-context )
@@ -605,21 +611,28 @@ def parse(self, response):
605611async def parse_in_new_context (self , response ):
606612 page = response.meta[" playwright_page" ]
607613 title = await page.title()
614+ await page.close()
608615 await page.context.close()
609616 return {" title" : title}
610617
611618async def close_context_on_error (self , failure ):
612619 page = failure.request.meta[" playwright_page" ]
620+ await page.close()
613621 await page.context.close()
614622```
615623
624+ ### Avoid race conditions & memory leaks when closing contexts
625+ Make sure to close the page before closing the context. See
626+ [ this comment] ( https://github.com/scrapy-plugins/scrapy-playwright/issues/191#issuecomment-1548097114 )
627+ in [ #191 ] ( https://github.com/scrapy-plugins/scrapy-playwright/issues/191 )
628+ for more information.
629+
616630### Maximum concurrent context count
617631
618632Specify a value for the ` PLAYWRIGHT_MAX_CONTEXTS ` setting to limit the amount
619633of concurent contexts. Use with caution: it's possible to block the whole crawl
620- if contexts are not closed after they are no longer used (refer to the above
621- section to dinamically close contexts, see also the section about
622- [ automatically closing inactive contexts] ( #automatically-closing-inactive-contexts ) ).
634+ if contexts are not closed after they are no longer used (refer to
635+ [ this section] ( #closing-contexts-while-crawling ) to dinamically close contexts).
623636Make sure to define an errback to still close contexts even if there are errors.
624637
625638
@@ -636,7 +649,7 @@ class ProxySpider(Spider):
636649 custom_settings = {
637650 " PLAYWRIGHT_LAUNCH_OPTIONS" : {
638651 " proxy" : {
639- " server" : " http://myproxy.com:3128"
652+ " server" : " http://myproxy.com:3128" ,
640653 " username" : " user" ,
641654 " password" : " pass" ,
642655 },
@@ -671,7 +684,7 @@ PLAYWRIGHT_CONTEXTS = {
671684}
672685```
673686
674- Or passing a ` proxy ` key when [ creating a context during a crawl ] ( #creating-a-context-during-a-crawl ) .
687+ Or passing a ` proxy ` key when [ creating contexts while crawling ] ( #creating-contexts-while-crawling ) .
675688
676689See also:
677690* [ ` zyte-smartproxy-playwright ` ] ( https://github.com/zytedata/zyte-smartproxy-playwright ) :
0 commit comments