From 79a911e24ed7658bbc3948e94dba34e0aa936247 Mon Sep 17 00:00:00 2001 From: Karim shoair Date: Mon, 16 Dec 2024 00:59:19 +0200 Subject: [PATCH] feat(StealthyFetcher): Add async fetch support --- scrapling/engines/camo.py | 74 ++++++++++++++++++++++++++++++++++++--- scrapling/fetchers.py | 58 ++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+), 4 deletions(-) diff --git a/scrapling/engines/camo.py b/scrapling/engines/camo.py index ec64e6d..5d22aa5 100644 --- a/scrapling/engines/camo.py +++ b/scrapling/engines/camo.py @@ -1,10 +1,12 @@ from camoufox import DefaultAddons +from camoufox.async_api import AsyncCamoufox from camoufox.sync_api import Camoufox from scrapling.core._types import (Callable, Dict, List, Literal, Optional, Union) from scrapling.core.utils import log from scrapling.engines.toolbelt import (Response, StatusText, + async_intercept_route, check_type_validity, construct_proxy_dict, generate_convincing_referer, @@ -120,11 +122,8 @@ class CamoufoxEngine: # This will be parsed inside `Response` encoding = res.headers.get('content-type', '') or 'utf-8' # default encoding - - status_text = res.status_text # PlayWright API sometimes give empty status text for some reason! - if not status_text: - status_text = StatusText.get(res.status) + status_text = res.status_text or StatusText.get(res.status) response = Response( url=res.url, @@ -141,3 +140,70 @@ class CamoufoxEngine: page.close() return response + + async def async_fetch(self, url: str) -> Response: + """Opens up the browser and do your request based on your chosen options. + + :param url: Target url. + :return: A `Response` object that is the same as `Adaptor` object except it has these added attributes: `status`, `reason`, `cookies`, `headers`, and `request_headers` + """ + addons = [] if self.disable_ads else [DefaultAddons.UBO] + async with AsyncCamoufox( + geoip=self.geoip, + proxy=self.proxy, + addons=self.addons, + exclude_addons=addons, + headless=self.headless, + humanize=self.humanize, + i_know_what_im_doing=True, # To turn warnings off with the user configurations + allow_webgl=self.allow_webgl, + block_webrtc=self.block_webrtc, + block_images=self.block_images, # Careful! it makes some websites doesn't finish loading at all like stackoverflow even in headful + os=None if self.os_randomize else get_os_name(), + ) as browser: + page = await browser.new_page() + page.set_default_navigation_timeout(self.timeout) + page.set_default_timeout(self.timeout) + if self.disable_resources: + await page.route("**/*", async_intercept_route) + + if self.extra_headers: + await page.set_extra_http_headers(self.extra_headers) + + res = await page.goto(url, referer=generate_convincing_referer(url) if self.google_search else None) + await page.wait_for_load_state(state="domcontentloaded") + if self.network_idle: + await page.wait_for_load_state('networkidle') + + if self.page_action is not None: + page = await self.page_action(page) + + if self.wait_selector and type(self.wait_selector) is str: + waiter = page.locator(self.wait_selector) + await waiter.first.wait_for(state=self.wait_selector_state) + # Wait again after waiting for the selector, helpful with protections like Cloudflare + await page.wait_for_load_state(state="load") + await page.wait_for_load_state(state="domcontentloaded") + if self.network_idle: + await page.wait_for_load_state('networkidle') + + # This will be parsed inside `Response` + encoding = res.headers.get('content-type', '') or 'utf-8' # default encoding + # PlayWright API sometimes give empty status text for some reason! + status_text = res.status_text or StatusText.get(res.status) + + response = Response( + url=res.url, + text=await page.content(), + body=(await page.content()).encode('utf-8'), + status=res.status, + reason=status_text, + encoding=encoding, + cookies={cookie['name']: cookie['value'] for cookie in await page.context.cookies()}, + headers=await res.all_headers(), + request_headers=await res.request.all_headers(), + **self.adaptor_arguments + ) + await page.close() + + return response diff --git a/scrapling/fetchers.py b/scrapling/fetchers.py index 6ba6689..86f77ae 100644 --- a/scrapling/fetchers.py +++ b/scrapling/fetchers.py @@ -230,6 +230,64 @@ class StealthyFetcher(BaseFetcher): ) return engine.fetch(url) + async def async_fetch( + self, url: str, headless: Optional[Union[bool, Literal['virtual']]] = True, block_images: Optional[bool] = False, disable_resources: Optional[bool] = False, + block_webrtc: Optional[bool] = False, allow_webgl: Optional[bool] = True, network_idle: Optional[bool] = False, addons: Optional[List[str]] = None, + timeout: Optional[float] = 30000, page_action: Callable = None, wait_selector: Optional[str] = None, humanize: Optional[Union[bool, float]] = True, + wait_selector_state: str = 'attached', google_search: Optional[bool] = True, extra_headers: Optional[Dict[str, str]] = None, proxy: Optional[Union[str, Dict[str, str]]] = None, + os_randomize: Optional[bool] = None, disable_ads: Optional[bool] = True, geoip: Optional[bool] = False, + ) -> Response: + """ + Opens up a browser and do your request based on your chosen options below. + + :param url: Target url. + :param headless: Run the browser in headless/hidden (default), 'virtual' screen mode, or headful/visible mode. + :param block_images: Prevent the loading of images through Firefox preferences. + This can help save your proxy usage but be careful with this option as it makes some websites never finish loading. + :param disable_resources: Drop requests of unnecessary resources for a speed boost. It depends but it made requests ~25% faster in my tests for some websites. + Requests dropped are of type `font`, `image`, `media`, `beacon`, `object`, `imageset`, `texttrack`, `websocket`, `csp_report`, and `stylesheet`. + This can help save your proxy usage but be careful with this option as it makes some websites never finish loading. + :param block_webrtc: Blocks WebRTC entirely. + :param addons: List of Firefox addons to use. Must be paths to extracted addons. + :param disable_ads: Enabled by default, this installs `uBlock Origin` addon on the browser if enabled. + :param humanize: Humanize the cursor movement. Takes either True or the MAX duration in seconds of the cursor movement. The cursor typically takes up to 1.5 seconds to move across the window. + :param allow_webgl: Enabled by default. Disabling it WebGL not recommended as many WAFs now checks if WebGL is enabled. + :param geoip: Recommended to use with proxies; Automatically use IP's longitude, latitude, timezone, country, locale, & spoof the WebRTC IP address. + It will also calculate and spoof the browser's language based on the distribution of language speakers in the target region. + :param network_idle: Wait for the page until there are no network connections for at least 500 ms. + :param os_randomize: If enabled, Scrapling will randomize the OS fingerprints used. The default is Scrapling matching the fingerprints with the current OS. + :param timeout: The timeout in milliseconds that is used in all operations and waits through the page. The default is 30000 + :param page_action: Added for automation. A function that takes the `page` object, does the automation you need, then returns `page` again. + :param wait_selector: Wait for a specific css selector to be in a specific state. + :param wait_selector_state: The state to wait for the selector given with `wait_selector`. Default state is `attached`. + :param google_search: Enabled by default, Scrapling will set the referer header to be as if this request came from a Google search for this website's domain name. + :param extra_headers: A dictionary of extra headers to add to the request. _The referer set by the `google_search` argument takes priority over the referer set here if used together._ + :param proxy: The proxy to be used with requests, it can be a string or a dictionary with the keys 'server', 'username', and 'password' only. + :return: A `Response` object that is the same as `Adaptor` object except it has these added attributes: `status`, `reason`, `cookies`, `headers`, and `request_headers` + """ + engine = CamoufoxEngine( + proxy=proxy, + geoip=geoip, + addons=addons, + timeout=timeout, + headless=headless, + humanize=humanize, + disable_ads=disable_ads, + allow_webgl=allow_webgl, + page_action=page_action, + network_idle=network_idle, + block_images=block_images, + block_webrtc=block_webrtc, + os_randomize=os_randomize, + wait_selector=wait_selector, + google_search=google_search, + extra_headers=extra_headers, + disable_resources=disable_resources, + wait_selector_state=wait_selector_state, + adaptor_arguments=self.adaptor_arguments, + ) + return await engine.async_fetch(url) + class PlayWrightFetcher(BaseFetcher): """A `Fetcher` class type that provide many options, all of them are based on PlayWright.