From d6a59774eab4c6e29b99c0c653e0dc5d7993a5bf Mon Sep 17 00:00:00 2001 From: Karim shoair Date: Tue, 26 Nov 2024 20:23:02 +0200 Subject: [PATCH] StealthyFetcher - The option to disable ads with `ublock origin` addon --- README.md | 1 + scrapling/engines/camo.py | 8 +++++++- scrapling/fetchers.py | 4 +++- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index de324cb..bf792c1 100644 --- a/README.md +++ b/README.md @@ -263,6 +263,7 @@ True | addons | List of Firefox addons to use. **Must be paths to extracted addons.** | ✔️ | | 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. | ✔️ | | allow_webgl | Whether to allow WebGL. To prevent leaks, only use this for special cases. | ✔️ | +| disable_ads | Enabled by default, this installs `uBlock Origin` addon on the browser if enabled. | ✔️ | | network_idle | Wait for the page until there are no network connections for at least 500 ms. | ✔️ | | timeout | The timeout in milliseconds that is used in all operations and waits through the page. The default is 30000. | ✔️ | | wait_selector | Wait for a specific css selector to be in a specific state. | ✔️ | diff --git a/scrapling/engines/camo.py b/scrapling/engines/camo.py index 0807a97..5853a18 100644 --- a/scrapling/engines/camo.py +++ b/scrapling/engines/camo.py @@ -12,6 +12,7 @@ from scrapling.engines.toolbelt import ( generate_convincing_referer, ) +from camoufox import DefaultAddons from camoufox.sync_api import Camoufox @@ -21,7 +22,8 @@ class CamoufoxEngine: block_webrtc: Optional[bool] = False, allow_webgl: Optional[bool] = False, network_idle: Optional[bool] = False, humanize: Optional[Union[bool, float]] = True, timeout: Optional[float] = 30000, page_action: Callable = do_nothing, wait_selector: Optional[str] = None, addons: Optional[List[str]] = None, 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, adaptor_arguments: Dict = None + proxy: Optional[Union[str, Dict[str, str]]] = None, os_randomize: Optional[bool] = None, disable_ads: Optional[bool] = True, + adaptor_arguments: Dict = None, ): """An engine that utilizes Camoufox library, check the `StealthyFetcher` class for more documentation. @@ -36,6 +38,7 @@ class CamoufoxEngine: :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: Whether to allow WebGL. To prevent leaks, only use this for special cases. :param network_idle: Wait for the page until there are no network connections for at least 500 ms. + :param disable_ads: Enabled by default, this installs `uBlock Origin` addon on the browser if enabled. :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. @@ -54,6 +57,7 @@ class CamoufoxEngine: self.network_idle = bool(network_idle) self.google_search = bool(google_search) self.os_randomize = bool(os_randomize) + self.disable_ads = bool(disable_ads) self.extra_headers = extra_headers or {} self.proxy = construct_proxy_dict(proxy) self.addons = addons or [] @@ -75,9 +79,11 @@ class CamoufoxEngine: :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] with Camoufox( 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 diff --git a/scrapling/fetchers.py b/scrapling/fetchers.py index 1e81ffd..e4e974f 100644 --- a/scrapling/fetchers.py +++ b/scrapling/fetchers.py @@ -78,7 +78,7 @@ class StealthyFetcher(BaseFetcher): block_webrtc: Optional[bool] = False, allow_webgl: Optional[bool] = False, network_idle: Optional[bool] = False, addons: Optional[List[str]] = None, timeout: Optional[float] = 30000, page_action: Callable = do_nothing, 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 + os_randomize: Optional[bool] = None, disable_ads: Optional[bool] = True, ) -> Response: """ Opens up a browser and do your request based on your chosen options below. @@ -92,6 +92,7 @@ class StealthyFetcher(BaseFetcher): 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: Whether to allow WebGL. To prevent leaks, only use this for special cases. :param network_idle: Wait for the page until there are no network connections for at least 500 ms. @@ -111,6 +112,7 @@ class StealthyFetcher(BaseFetcher): timeout=timeout, headless=headless, humanize=humanize, + disable_ads=disable_ads, allow_webgl=allow_webgl, page_action=page_action, network_idle=network_idle,