{"id":18402186,"url":"https://github.com/gerapy/gerapyplaywright","last_synced_at":"2025-05-08T21:12:21.325Z","repository":{"id":43356564,"uuid":"442110583","full_name":"Gerapy/GerapyPlaywright","owner":"Gerapy","description":"Downloader Middleware to support Playwright in Scrapy \u0026 Gerapy","archived":false,"fork":false,"pushed_at":"2022-03-06T04:58:48.000Z","size":92,"stargazers_count":113,"open_issues_count":13,"forks_count":21,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-08T21:11:37.798Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Gerapy.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-12-27T09:19:43.000Z","updated_at":"2025-04-27T03:05:24.000Z","dependencies_parsed_at":"2022-08-20T02:40:43.346Z","dependency_job_id":null,"html_url":"https://github.com/Gerapy/GerapyPlaywright","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gerapy%2FGerapyPlaywright","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gerapy%2FGerapyPlaywright/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gerapy%2FGerapyPlaywright/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gerapy%2FGerapyPlaywright/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Gerapy","download_url":"https://codeload.github.com/Gerapy/GerapyPlaywright/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253149617,"owners_count":21861739,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-06T02:41:33.468Z","updated_at":"2025-05-08T21:12:21.098Z","avatar_url":"https://github.com/Gerapy.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Gerapy Playwright\n\nThis is a package for supporting Playwright in Scrapy, also this\npackage is a module in [Gerapy](https://github.com/Gerapy/Gerapy).\n\n## Note\n\n**This package does not work on Windows**, please check this [issue](https://github.com/Gerapy/GerapyPlaywright/issues/2), please use this Package on Linux/Mac currently.\n\n## Installation\n\n```shell script\npip3 install gerapy-playwright\nplaywright install\n```\n\n## Usage\n\nYou can use `PlaywrightRequest` to specify a request which uses playwright to render.\n\nFor example:\n\n```python\nyield PlaywrightRequest(detail_url, callback=self.parse_detail)\n```\n\nAnd you also need to enable `PlaywrightMiddleware` in `DOWNLOADER_MIDDLEWARES`:\n\n```python\nDOWNLOADER_MIDDLEWARES = {\n    'gerapy_playwright.downloadermiddlewares.PlaywrightMiddleware': 543,\n}\n```\n\nCongratulate, you've finished the all of the required configuration.\n\nIf you run the Spider again, Playwright will be started to render every\nweb page which you configured the request as PlaywrightRequest.\n\n## Settings\n\nGerapyPlaywright provides some optional settings.\n\n### Concurrency\n\nYou can directly use Scrapy's setting to set Concurrency of Playwright,\nfor example:\n\n```python\nCONCURRENT_REQUESTS = 3\n```\n\n### Pretend as Real Browser\n\nSome website will detect WebDriver or Headless, GerapyPlaywright can\npretend Chromium by inject scripts. This is enabled by default.\n\nYou can close it if website does not detect WebDriver to speed up:\n\n```python\nGERAPY_PLAYWRIGHT_PRETEND = False\n```\n\nAlso you can use `pretend` attribute in `PlaywrightRequest` to overwrite this\nconfiguration.\n\n### Logging Level\n\nBy default, Playwright will log all the debug messages, so GerapyPlaywright\nconfigured the logging level of Playwright to WARNING.\n\nIf you want to see more logs from Playwright, you can change the this setting:\n\n```python\nimport logging\nGERAPY_PLAYWRIGHT_LOGGING_LEVEL = logging.DEBUG\n```\n\n### Download Timeout\n\nPlaywright may take some time to render the required web page, you can also change this setting, default is `30s`:\n\n```python\n# playwright timeout\nGERAPY_PLAYWRIGHT_DOWNLOAD_TIMEOUT = 30\n```\n\n### Headless\n\nBy default, Playwright is running in `Headless` mode, you can also\nchange it to `False` as you need, default is `True`:\n\n```python\nGERAPY_PLAYWRIGHT_HEADLESS = False\n```\n\n### Window Size\n\nYou can also set the width and height of Playwright window:\n\n```python\nGERAPY_PLAYWRIGHT_WINDOW_WIDTH = 1400\nGERAPY_PLAYWRIGHT_WINDOW_HEIGHT = 700\n```\n\nDefault is 1400, 700.\n\n### Proxy\n\nYou can set a proxy channel via below this config:\n\n```python\nGERAPY_PLAYWRIGHT_PROXY = 'http://tps254.kdlapi.com:15818'\nGERAPY_PLAYWRIGHT_PROXY_CREDENTIAL = {\n  'username': 'xxx',\n  'password': 'xxxx'\n}\n```\n\n### Screenshot\n\nYou can get screenshot of loaded page, you can pass `screenshot` args to `PlaywrightRequest` as dict:\n\nBelow are the supported args:\n\n- `type` (str): Specify screenshot type, can be either `jpeg` or `png`. Defaults to `png`.\n- `quality` (int): The quality of the image, between 0-100. Not applicable to `png` image.\n- `full_page` (bool): When true, take a screenshot of the full scrollable page. Defaults to `False`.\n- `clip` (dict): An object which specifies clipping region of the page. This option should have the following fields:\n  - `x` (int): x-coordinate of top-left corner of clip area.\n  - `y` (int): y-coordinate of top-left corner of clip area.\n  - `width` (int): width of clipping area.\n  - `height` (int): height of clipping area.\n- `omit_background` (bool): Hide default white background and allow capturing screenshot with transparency.\n- `timeout` (str): Maximum time in milliseconds, defaults to 30 seconds, pass 0 to disable timeout.\n\nCheck more from [https://playwright.dev/python/docs/api/class-page#page-screenshot](https://playwright.dev/python/docs/api/class-page#page-screenshot)。\n\nFor example:\n\n```python\nyield PlaywrightRequest(start_url, callback=self.parse_index, wait_for='.item .name', screenshot={\n            'type': 'png',\n            'full_page': True\n        })\n```\n\nthen you can get screenshot result in `response.meta['screenshot']`:\n\nSimplest save it to file:\n\n```python\ndef parse_index(self, response):\n    with open('screenshot.png', 'wb') as f:\n        f.write(response.meta['screenshot'].getbuffer())\n```\n\nIf you want to enable screenshot for all requests, you can configure it by `GERAPY_PLAYWRIGHT_SCREENSHOT`.\n\nFor example:\n\n```python\nGERAPY_PLAYWRIGHT_SCREENSHOT = {\n    'type': 'png',\n    'full_page': True\n}\n```\n\n## PlaywrightRequest\n\n`PlaywrightRequest` provide args which can override global settings above.\n\n- url: request url\n- callback: callback\n- wait_until: one of \"load\", \"domcontentloaded\", \"networkidle\"\n  see [https://playwright.dev/python/docs/api/class-page#page-wait-for-load-state](https://playwright.dev/python/docs/api/class-page#page-wait-for-load-state), default is `domcontentloaded`\n- wait_for: wait for some element to load, also supports dict\n- script: script to execute\n- actions: actions defined for execution of Page object\n- proxy: use proxy for this time, like `http://x.x.x.x:x`\n- proxy_credential: the proxy credential, like `{'username': 'xxxx', 'password': 'xxxx'}`\n- sleep: time to sleep after loaded, override `GERAPY_PLAYWRIGHT_SLEEP`\n- timeout: load timeout, override `GERAPY_PLAYWRIGHT_DOWNLOAD_TIMEOUT`\n- ignore_resource_types: ignored resource types, override `GERAPY_PLAYWRIGHT_IGNORE_RESOURCE_TYPES`\n- pretend: pretend as normal browser, override `GERAPY_PLAYWRIGHT_PRETEND`\n- screenshot: ignored resource types, see [https://playwright.dev/python/docs/api/class-page#page-screenshot](https://playwright.dev/python/docs/api/class-page#page-screenshot),\n  override `GERAPY_PLAYWRIGHT_SCREENSHOT`\n\nFor example, you can configure PlaywrightRequest as:\n\n```python\nfrom gerapy_playwright import PlaywrightRequest\n\ndef parse(self, response):\n    yield PlaywrightRequest(url,\n        callback=self.parse_detail,\n        wait_until='domcontentloaded',\n        wait_for='title',\n        script='() =\u003e { return {name: \"Germey\"} }',\n        sleep=2)\n```\n\nThen Playwright will:\n\n- wait for document to load\n- wait for title to load\n- execute `console.log(document)` script\n- sleep for 2s\n- return the rendered web page content, get from `response.meta['screenshot']`\n- return the script executed result, get from `response.meta['script_result']`\n\nFor waiting mechanism controlled by JavaScript, you can use await in `script`, for example:\n\n```python\njs = '''async () =\u003e {\n    await new Promise(resolve =\u003e setTimeout(resolve, 10000));\n    return {\n        'name': 'Germey'\n    }\n}\n'''\nyield PlaywrightRequest(url, callback=self.parse, script=js)\n```\n\nThen you can get the script result from `response.meta['script_result']`, result is `{'name': 'Germey'}`.\n\nIf you think the JavaScript is wired to write, you can use actions argument to define a function to execute `Python` based functions, for example:\n\n```python\nasync def execute_actions(page):\n    await page.evaluate('() =\u003e { document.title = \"Hello World\"; }')\n    return 1\nyield PlaywrightRequest(url, callback=self.parse, actions=execute_actions)\n```\n\nThen you can get the actions result from `response.meta['actions_result']`, result is `1`.\n\nAlso you can define proxy and proxy_credential for each Reqest, for example:\n\n```python\nyield PlaywrightRequest(\n  self.base_url,\n  callback=self.parse_index,\n  priority=10,\n  proxy='http://tps254.kdlapi.com:15818',\n  proxy_credential={\n      'username': 'xxxx',\n      'password': 'xxxx'\n})\n```\n\n`proxy` and `proxy_credential` will override the settings `GERAPY_PLAYWRIGHT_PROXY` and `GERAPY_PLAYWRIGHT_PROXY_CREDENTIAL`.\n\n## Example\n\nFor more detail, please see [example](./example).\n\nAlso you can directly run with Docker:\n\n```shell\ndocker run germey/gerapy-playwright-example\n```\n\nOutputs:\n\n```shell script\n2021-12-27 16:54:14 [scrapy.utils.log] INFO: Scrapy 2.2.0 started (bot: example)\n2021-12-27 16:54:14 [scrapy.utils.log] INFO: Versions: lxml 4.7.1.0, libxml2 2.9.12, cssselect 1.1.0, parsel 1.6.0, w3lib 1.22.0, Twisted 21.7.0, Python 3.7.9 (default, Aug 31 2020, 07:22:35) - [Clang 10.0.0 ], pyOpenSSL 21.0.0 (OpenSSL 1.1.1l  24 Aug 2021), cryptography 35.0.0, Platform Darwin-21.1.0-x86_64-i386-64bit\n2021-12-27 16:54:14 [scrapy.utils.log] DEBUG: Using reactor: twisted.internet.asyncioreactor.AsyncioSelectorReactor\n2021-12-27 16:54:14 [scrapy.crawler] INFO: Overridden settings:\n{'BOT_NAME': 'example',\n 'CONCURRENT_REQUESTS': 1,\n 'NEWSPIDER_MODULE': 'example.spiders',\n 'RETRY_HTTP_CODES': [403, 500, 502, 503, 504],\n 'SPIDER_MODULES': ['example.spiders']}\n2021-12-27 16:54:14 [scrapy.extensions.telnet] INFO: Telnet Password: e931b241390ad06a\n2021-12-27 16:54:14 [scrapy.middleware] INFO: Enabled extensions:\n['scrapy.extensions.corestats.CoreStats',\n 'scrapy.extensions.telnet.TelnetConsole',\n 'scrapy.extensions.memusage.MemoryUsage',\n 'scrapy.extensions.logstats.LogStats']\n2021-12-27 16:54:14 [gerapy.playwright] INFO: playwright libraries already installed\n2021-12-27 16:54:14 [scrapy.middleware] INFO: Enabled downloader middlewares:\n['scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',\n 'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',\n 'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',\n 'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',\n 'gerapy_playwright.downloadermiddlewares.PlaywrightMiddleware',\n 'scrapy.downloadermiddlewares.retry.RetryMiddleware',\n 'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',\n 'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',\n 'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',\n 'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',\n 'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware',\n 'scrapy.downloadermiddlewares.stats.DownloaderStats']\n2021-12-27 16:54:14 [scrapy.middleware] INFO: Enabled spider middlewares:\n['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',\n 'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',\n 'scrapy.spidermiddlewares.referer.RefererMiddleware',\n 'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',\n 'scrapy.spidermiddlewares.depth.DepthMiddleware']\n2021-12-27 16:54:14 [scrapy.middleware] INFO: Enabled item pipelines:\n[]\n2021-12-27 16:54:14 [scrapy.core.engine] INFO: Spider opened\n2021-12-27 16:54:14 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)\n2021-12-27 16:54:14 [scrapy.extensions.telnet] INFO: Telnet console listening on 127.0.0.1:6023\n2021-12-27 16:54:14 [example.spiders.movie] DEBUG: start url https://antispider1.scrape.center/page/1\n2021-12-27 16:54:14 [gerapy.playwright] DEBUG: processing request \u003cGET https://antispider1.scrape.center/page/1\u003e\n2021-12-27 16:54:14 [gerapy.playwright] DEBUG: playwright_meta {'wait_until': 'domcontentloaded', 'wait_for': '.item', 'script': None, 'actions': None, 'sleep': None, 'proxy': None, 'proxy_credential': None, 'pretend': None, 'timeout': None, 'screenshot': None}\n2021-12-27 16:54:14 [gerapy.playwright] DEBUG: set options {'headless': False}\ncookies []\n2021-12-27 16:54:16 [gerapy.playwright] DEBUG: PRETEND_SCRIPTS is run\n2021-12-27 16:54:16 [gerapy.playwright] DEBUG: timeout 10\n2021-12-27 16:54:16 [gerapy.playwright] DEBUG: crawling https://antispider1.scrape.center/page/1\n2021-12-27 16:54:16 [gerapy.playwright] DEBUG: request https://antispider1.scrape.center/page/1 with options {'url': 'https://antispider1.scrape.center/page/1', 'wait_until': 'domcontentloaded'}\n2021-12-27 16:54:18 [gerapy.playwright] DEBUG: waiting for .item\n2021-12-27 16:54:18 [gerapy.playwright] DEBUG: sleep for 1s\n2021-12-27 16:54:19 [gerapy.playwright] DEBUG: taking screenshot using args {'type': 'png', 'full_page': True}\n2021-12-27 16:54:19 [gerapy.playwright] DEBUG: close playwright\n2021-12-27 16:54:20 [scrapy.core.engine] DEBUG: Crawled (200) \u003cGET https://antispider1.scrape.center/page/1\u003e (referer: None)\n2021-12-27 16:54:20 [example.spiders.movie] DEBUG: start url https://antispider1.scrape.center/page/2\n2021-12-27 16:54:20 [gerapy.playwright] DEBUG: processing request \u003cGET https://antispider1.scrape.center/page/2\u003e\n2021-12-27 16:54:20 [gerapy.playwright] DEBUG: playwright_meta {'wait_until': 'domcontentloaded', 'wait_for': '.item', 'script': None, 'actions': None, 'sleep': None, 'proxy': None, 'proxy_credential': None, 'pretend': None, 'timeout': None, 'screenshot': None}\n2021-12-27 16:54:20 [gerapy.playwright] DEBUG: set options {'headless': False}\n2021-12-27 16:54:20 [example.spiders.movie] INFO: detail url https://antispider1.scrape.center/detail/1\n2021-12-27 16:54:20 [example.spiders.movie] INFO: detail url https://antispider1.scrape.center/detail/2\n2021-12-27 16:54:20 [example.spiders.movie] INFO: detail url https://antispider1.scrape.center/detail/3\n2021-12-27 16:54:20 [example.spiders.movie] INFO: detail url https://antispider1.scrape.center/detail/4\n2021-12-27 16:54:20 [example.spiders.movie] INFO: detail url https://antispider1.scrape.center/detail/5\n2021-12-27 16:54:20 [example.spiders.movie] INFO: detail url https://antispider1.scrape.center/detail/6\n2021-12-27 16:54:20 [example.spiders.movie] INFO: detail url https://antispider1.scrape.center/detail/7\n2021-12-27 16:54:20 [example.spiders.movie] INFO: detail url https://antispider1.scrape.center/detail/8\n2021-12-27 16:54:20 [example.spiders.movie] INFO: detail url https://antispider1.scrape.center/detail/9\n2021-12-27 16:54:20 [example.spiders.movie] INFO: detail url https://antispider1.scrape.center/detail/10\ncookies []\n2021-12-27 16:54:21 [gerapy.playwright] DEBUG: PRETEND_SCRIPTS is run\n2021-12-27 16:54:21 [gerapy.playwright] DEBUG: timeout 10\n2021-12-27 16:54:21 [gerapy.playwright] DEBUG: crawling https://antispider1.scrape.center/page/2\n2021-12-27 16:54:21 [gerapy.playwright] DEBUG: request https://antispider1.scrape.center/page/2 with options {'url': 'https://antispider1.scrape.center/page/2', 'wait_until': 'domcontentloaded'}\n2021-12-27 16:54:23 [gerapy.playwright] DEBUG: waiting for .item\n2021-12-27 16:54:24 [gerapy.playwright] DEBUG: sleep for 1s\n2021-12-27 16:54:25 [gerapy.playwright] DEBUG: taking screenshot using args {'type': 'png', 'full_page': True}\n2021-12-27 16:54:25 [gerapy.playwright] DEBUG: close playwright\n2021-12-27 16:54:25 [scrapy.core.engine] DEBUG: Crawled (200) \u003cGET https://antispider1.scrape.center/page/2\u003e (referer: None)\n2021-12-27 16:54:25 [gerapy.playwright] DEBUG: processing request \u003cGET https://antispider1.scrape.center/detail/10\u003e\n2021-12-27 16:54:25 [gerapy.playwright] DEBUG: playwright_meta {'wait_until': 'domcontentloaded', 'wait_for': '.item', 'script': None, 'actions': None, 'sleep': None, 'proxy': None, 'proxy_credential': None, 'pretend': None, 'timeout': None, 'screenshot': None}\n2021-12-27 16:54:25 [gerapy.playwright] DEBUG: set options {'headless': False}\n...\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgerapy%2Fgerapyplaywright","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgerapy%2Fgerapyplaywright","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgerapy%2Fgerapyplaywright/lists"}