Karim shoair commited on
Commit ·
8ff23b3
1
Parent(s): c1fb967
fix(browsers): solving an issues with leaving playwright loop open when cdp connection fails
Browse files
scrapling/engines/_browsers/_controllers.py
CHANGED
|
@@ -3,13 +3,11 @@ from asyncio import sleep as asyncio_sleep
|
|
| 3 |
|
| 4 |
from playwright.sync_api import (
|
| 5 |
Locator,
|
| 6 |
-
Playwright,
|
| 7 |
sync_playwright,
|
| 8 |
)
|
| 9 |
from playwright.async_api import (
|
| 10 |
async_playwright,
|
| 11 |
Locator as AsyncLocator,
|
| 12 |
-
Playwright as AsyncPlaywright,
|
| 13 |
BrowserContext as AsyncBrowserContext,
|
| 14 |
)
|
| 15 |
|
|
@@ -71,21 +69,27 @@ class DynamicSession(SyncSession, DynamicSessionMixin):
|
|
| 71 |
def start(self):
|
| 72 |
"""Create a browser for this instance and context."""
|
| 73 |
if not self.playwright:
|
| 74 |
-
self.playwright
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
else:
|
| 90 |
raise RuntimeError("Session has been already started")
|
| 91 |
|
|
@@ -209,23 +213,28 @@ class AsyncDynamicSession(AsyncSession, DynamicSessionMixin):
|
|
| 209 |
async def start(self):
|
| 210 |
"""Create a browser for this instance and context."""
|
| 211 |
if not self.playwright:
|
| 212 |
-
self.playwright
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
else:
|
| 230 |
raise RuntimeError("Session has been already started")
|
| 231 |
|
|
|
|
| 3 |
|
| 4 |
from playwright.sync_api import (
|
| 5 |
Locator,
|
|
|
|
| 6 |
sync_playwright,
|
| 7 |
)
|
| 8 |
from playwright.async_api import (
|
| 9 |
async_playwright,
|
| 10 |
Locator as AsyncLocator,
|
|
|
|
| 11 |
BrowserContext as AsyncBrowserContext,
|
| 12 |
)
|
| 13 |
|
|
|
|
| 69 |
def start(self):
|
| 70 |
"""Create a browser for this instance and context."""
|
| 71 |
if not self.playwright:
|
| 72 |
+
self.playwright = sync_playwright().start()
|
| 73 |
|
| 74 |
+
try:
|
| 75 |
+
if self._config.cdp_url: # pragma: no cover
|
| 76 |
+
browser = self.playwright.chromium.connect_over_cdp(endpoint_url=self._config.cdp_url)
|
| 77 |
+
self.context = browser.new_context(**self._context_options)
|
| 78 |
+
else:
|
| 79 |
+
self.context = self.playwright.chromium.launch_persistent_context(**self._launch_options)
|
| 80 |
|
| 81 |
+
if self._config.init_script: # pragma: no cover
|
| 82 |
+
self.context.add_init_script(path=self._config.init_script)
|
| 83 |
|
| 84 |
+
if self._config.cookies: # pragma: no cover
|
| 85 |
+
self.context.add_cookies(self._config.cookies)
|
| 86 |
|
| 87 |
+
self._is_alive = True
|
| 88 |
+
except Exception:
|
| 89 |
+
# Clean up playwright if browser setup fails
|
| 90 |
+
self.playwright.stop()
|
| 91 |
+
self.playwright = None
|
| 92 |
+
raise
|
| 93 |
else:
|
| 94 |
raise RuntimeError("Session has been already started")
|
| 95 |
|
|
|
|
| 213 |
async def start(self):
|
| 214 |
"""Create a browser for this instance and context."""
|
| 215 |
if not self.playwright:
|
| 216 |
+
self.playwright = await async_playwright().start()
|
| 217 |
+
try:
|
| 218 |
+
if self._config.cdp_url:
|
| 219 |
+
browser = await self.playwright.chromium.connect_over_cdp(endpoint_url=self._config.cdp_url)
|
| 220 |
+
self.context: AsyncBrowserContext = await browser.new_context(**self._context_options)
|
| 221 |
+
else:
|
| 222 |
+
self.context: AsyncBrowserContext = await self.playwright.chromium.launch_persistent_context(
|
| 223 |
+
**self._launch_options
|
| 224 |
+
)
|
| 225 |
+
|
| 226 |
+
if self._config.init_script: # pragma: no cover
|
| 227 |
+
await self.context.add_init_script(path=self._config.init_script)
|
| 228 |
+
|
| 229 |
+
if self._config.cookies:
|
| 230 |
+
await self.context.add_cookies(self._config.cookies) # pyright: ignore
|
| 231 |
+
|
| 232 |
+
self._is_alive = True
|
| 233 |
+
except Exception:
|
| 234 |
+
# Clean up playwright if browser setup fails
|
| 235 |
+
await self.playwright.stop()
|
| 236 |
+
self.playwright = None
|
| 237 |
+
raise
|
| 238 |
else:
|
| 239 |
raise RuntimeError("Session has been already started")
|
| 240 |
|
scrapling/engines/_browsers/_stealth.py
CHANGED
|
@@ -6,12 +6,10 @@ from asyncio import sleep as asyncio_sleep
|
|
| 6 |
from playwright.sync_api import (
|
| 7 |
Locator,
|
| 8 |
Page,
|
| 9 |
-
Playwright,
|
| 10 |
)
|
| 11 |
from playwright.async_api import (
|
| 12 |
Page as async_Page,
|
| 13 |
Locator as AsyncLocator,
|
| 14 |
-
Playwright as AsyncPlaywright,
|
| 15 |
BrowserContext as AsyncBrowserContext,
|
| 16 |
)
|
| 17 |
from patchright.sync_api import sync_playwright
|
|
@@ -82,24 +80,30 @@ class StealthySession(SyncSession, StealthySessionMixin):
|
|
| 82 |
def start(self):
|
| 83 |
"""Create a browser for this instance and context."""
|
| 84 |
if not self.playwright:
|
| 85 |
-
self.playwright
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
else:
|
| 104 |
raise RuntimeError("Session has been already started")
|
| 105 |
|
|
@@ -308,26 +312,31 @@ class AsyncStealthySession(AsyncSession, StealthySessionMixin):
|
|
| 308 |
async def start(self):
|
| 309 |
"""Create a browser for this instance and context."""
|
| 310 |
if not self.playwright:
|
| 311 |
-
self.playwright
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
|
| 321 |
-
|
| 322 |
-
|
| 323 |
|
| 324 |
-
|
| 325 |
-
|
| 326 |
|
| 327 |
-
|
| 328 |
-
|
| 329 |
|
| 330 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 331 |
else:
|
| 332 |
raise RuntimeError("Session has been already started")
|
| 333 |
|
|
|
|
| 6 |
from playwright.sync_api import (
|
| 7 |
Locator,
|
| 8 |
Page,
|
|
|
|
| 9 |
)
|
| 10 |
from playwright.async_api import (
|
| 11 |
Page as async_Page,
|
| 12 |
Locator as AsyncLocator,
|
|
|
|
| 13 |
BrowserContext as AsyncBrowserContext,
|
| 14 |
)
|
| 15 |
from patchright.sync_api import sync_playwright
|
|
|
|
| 80 |
def start(self):
|
| 81 |
"""Create a browser for this instance and context."""
|
| 82 |
if not self.playwright:
|
| 83 |
+
self.playwright = sync_playwright().start()
|
| 84 |
|
| 85 |
+
try:
|
| 86 |
+
if self._config.cdp_url: # pragma: no cover
|
| 87 |
+
browser = self.playwright.chromium.connect_over_cdp(endpoint_url=self._config.cdp_url)
|
| 88 |
+
self.context = browser.new_context(**self._context_options)
|
| 89 |
+
else:
|
| 90 |
+
self.context = self.playwright.chromium.launch_persistent_context(**self._launch_options)
|
| 91 |
|
| 92 |
+
for script in _compiled_stealth_scripts():
|
| 93 |
+
self.context.add_init_script(script=script)
|
| 94 |
|
| 95 |
+
if self._config.init_script: # pragma: no cover
|
| 96 |
+
self.context.add_init_script(path=self._config.init_script)
|
| 97 |
|
| 98 |
+
if self._config.cookies: # pragma: no cover
|
| 99 |
+
self.context.add_cookies(self._config.cookies)
|
| 100 |
|
| 101 |
+
self._is_alive = True
|
| 102 |
+
except Exception:
|
| 103 |
+
# Clean up playwright if browser setup fails
|
| 104 |
+
self.playwright.stop()
|
| 105 |
+
self.playwright = None
|
| 106 |
+
raise
|
| 107 |
else:
|
| 108 |
raise RuntimeError("Session has been already started")
|
| 109 |
|
|
|
|
| 312 |
async def start(self):
|
| 313 |
"""Create a browser for this instance and context."""
|
| 314 |
if not self.playwright:
|
| 315 |
+
self.playwright = await async_playwright().start()
|
| 316 |
+
try:
|
| 317 |
+
if self._config.cdp_url:
|
| 318 |
+
browser = await self.playwright.chromium.connect_over_cdp(endpoint_url=self._config.cdp_url)
|
| 319 |
+
self.context: AsyncBrowserContext = await browser.new_context(**self._context_options)
|
| 320 |
+
else:
|
| 321 |
+
self.context: AsyncBrowserContext = await self.playwright.chromium.launch_persistent_context(
|
| 322 |
+
**self._launch_options
|
| 323 |
+
)
|
| 324 |
|
| 325 |
+
for script in _compiled_stealth_scripts():
|
| 326 |
+
await self.context.add_init_script(script=script)
|
| 327 |
|
| 328 |
+
if self._config.init_script: # pragma: no cover
|
| 329 |
+
await self.context.add_init_script(path=self._config.init_script)
|
| 330 |
|
| 331 |
+
if self._config.cookies:
|
| 332 |
+
await self.context.add_cookies(self._config.cookies) # pyright: ignore
|
| 333 |
|
| 334 |
+
self._is_alive = True
|
| 335 |
+
except Exception:
|
| 336 |
+
# Clean up playwright if browser setup fails
|
| 337 |
+
await self.playwright.stop()
|
| 338 |
+
self.playwright = None
|
| 339 |
+
raise
|
| 340 |
else:
|
| 341 |
raise RuntimeError("Session has been already started")
|
| 342 |
|