sarperavci / CloudflareBypassForScraping

A cloudflare verification bypass script for webscraping
595 stars 110 forks source link

why i can't open bulk browsers at same time #40

Open salahhadjar opened 2 months ago

salahhadjar commented 2 months ago

am trying to open like 5 already chrome browsers ''already created one" but the problem is that it only monito one chrome browser it doesnt monitor them all is there any solution ? image

sarperavci commented 1 month ago

You can fix your issue with this setting.

https://github.com/sarperavci/CloudflareBypassForScraping/issues/5#issuecomment-2342488730

salahhadjar commented 1 month ago

could you find the problem in my code here import time import logging import threading import random import socket from CloudflareBypasser import CloudflareBypasser from DrissionPage import ChromiumPage, ChromiumOptions

def setup_logging(browser_index): log_file = f'cloudflare_bypassbrowser{browser_index}.log' logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', handlers=[ logging.StreamHandler(), logging.FileHandler(logfile, mode='w') ] ) return logging.getLogger(f'BrowserLogger{browser_index}')

def find_free_port(): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.bind(('', 0)) return s.getsockname()[1]

def get_chromium_options(browser_path: str, user_data_dir: str) -> ChromiumOptions: options = ChromiumOptions() options.set_paths(browser_path=browser_path) options.set_argument(f'--user-data-dir={user_data_dir}') port = find_free_port() options.set_argument(f'--remote-debugging-port={port}') options.set_argument('--no-sandbox') options.set_argument('--disable-dev-shm-usage') return options

def check_for_cloudflare(driver, logger, profile_name): while True: try: time.sleep(10)

        if "Just a moment..." in driver.html:
            logger.info(f"Cloudflare challenge detected on {profile_name}. Attempting to solve...")
            cf_bypasser = CloudflareBypasser(driver)
            cf_bypasser.bypass()
            logger.info(f"Successfully bypassed Cloudflare on {profile_name}.")
        else:
            logger.info(f"No Cloudflare challenge detected on {profile_name}.")

    except Exception as e:
        logger.error(f"Error while checking Cloudflare in {profile_name}: {str(e)}")
        time.sleep(5)

def bypass_cloudflare(url: str, browser_path: str, user_data_dir: str, browser_index: int): logger = setup_logging(browser_index) options = get_chromium_options(browser_path, user_data_dir)

driver = ChromiumPage(addr_or_opts=options)
try:
    logger.info(f'Navigating to {url}.')
    driver.get(url)

    check_for_cloudflare(driver, logger, f'Profile {browser_index}')

except Exception as e:
    logger.error("An error occurred: %s", str(e))
finally:
    logger.info('Monitoring completed for Profile %d. Browser remains open.', browser_index)

def main(): chrome_paths = [ r'C:\CF\GoogleChrome\New folder\CHROME 1\chrome.exe', r'C:\CF\GoogleChrome\New folder\CHROME 2\chrome.exe', r'C:\CF\GoogleChrome\New folder\CHROME 3\chrome.exe', r'C:\CF\GoogleChrome\New folder\CHROME 4\chrome.exe', ]

user_data_dirs = [
    r'C:\CF\GoogleChrome\New folder\CHROME 1\Data\profile',
    r'C:\CF\GoogleChrome\New folder\CHROME 2\Data\profile',
    r'C:\CF\GoogleChrome\New folder\CHROME 3\Data\profile',
    r'C:\CF\GoogleChrome\New folder\CHROME 4\Data\profile',
]

url = 'https://nopecha.com/demo/cloudflare'

threads = []
for i in range(len(chrome_paths)):
    chrome_path = chrome_paths[i]
    user_data_dir = user_data_dirs[i]
    thread = threading.Thread(target=bypass_cloudflare, args=(url, chrome_path, user_data_dir, i + 1))
    threads.append(thread)
    thread.start()

for thread in threads:
    thread.join()

if name == 'main': main() thanks in advance

sarperavci commented 1 month ago

What's the error exactly you get? Can you share a console output or a screenshot?

On Fri, Oct 4, 2024, 18:09 Jockerdz Salah @.***> wrote:

could you find the problem in my code here import time import logging import threading import random import socket from CloudflareBypasser import CloudflareBypasser from DrissionPage import ChromiumPage, ChromiumOptions

def setup_logging(browser_index): log_file = f'cloudflare_bypassbrowser{browser_index}.log' logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', handlers=[ logging.StreamHandler(), logging.FileHandler(logfile, mode='w') ] ) return logging.getLogger(f'BrowserLogger{browser_index}')

def find_free_port(): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.bind(('', 0)) return s.getsockname()[1]

def get_chromium_options(browser_path: str, user_data_dir: str) -> ChromiumOptions: options = ChromiumOptions() options.set_paths(browser_path=browser_path) options.set_argument(f'--user-data-dir={user_data_dir}') port = find_free_port() options.set_argument(f'--remote-debugging-port={port}') options.set_argument('--no-sandbox') options.set_argument('--disable-dev-shm-usage') return options

def check_for_cloudflare(driver, logger, profile_name): while True: try: time.sleep(10)

    if "Just a moment..." in driver.html:
        logger.info(f"Cloudflare challenge detected on {profile_name}. Attempting to solve...")
        cf_bypasser = CloudflareBypasser(driver)
        cf_bypasser.bypass()
        logger.info(f"Successfully bypassed Cloudflare on {profile_name}.")
    else:
        logger.info(f"No Cloudflare challenge detected on {profile_name}.")

except Exception as e:
    logger.error(f"Error while checking Cloudflare in {profile_name}: {str(e)}")
    time.sleep(5)

def bypass_cloudflare(url: str, browser_path: str, user_data_dir: str, browser_index: int): logger = setup_logging(browser_index) options = get_chromium_options(browser_path, user_data_dir)

driver = ChromiumPage(addr_or_opts=options) try: logger.info(f'Navigating to {url}.') driver.get(url)

check_for_cloudflare(driver, logger, f'Profile {browser_index}')

except Exception as e: logger.error("An error occurred: %s", str(e)) finally: logger.info('Monitoring completed for Profile %d. Browser remains open.', browser_index)

def main(): chrome_paths = [ r'C:\CF\GoogleChrome\New folder\CHROME 1\chrome.exe', r'C:\CF\GoogleChrome\New folder\CHROME 2\chrome.exe', r'C:\CF\GoogleChrome\New folder\CHROME 3\chrome.exe', r'C:\CF\GoogleChrome\New folder\CHROME 4\chrome.exe', ]

user_data_dirs = [ r'C:\CF\GoogleChrome\New folder\CHROME 1\Data\profile', r'C:\CF\GoogleChrome\New folder\CHROME 2\Data\profile', r'C:\CF\GoogleChrome\New folder\CHROME 3\Data\profile', r'C:\CF\GoogleChrome\New folder\CHROME 4\Data\profile', ]

url = 'https://nopecha.com/demo/cloudflare'

threads = [] for i in range(len(chrome_paths)): chrome_path = chrome_paths[i] user_data_dir = user_data_dirs[i] thread = threading.Thread(target=bypass_cloudflare, args=(url, chrome_path, user_data_dir, i + 1)) threads.append(thread) thread.start()

for thread in threads: thread.join()

if name == 'main': main() thanks in advance

— Reply to this email directly, view it on GitHub https://github.com/sarperavci/CloudflareBypassForScraping/issues/40#issuecomment-2393927196, or unsubscribe https://github.com/notifications/unsubscribe-auth/AL7KOEF3XIL4S3TNORMZG23ZZ2VTNAVCNFSM6AAAAABO3RZPWGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGOJTHEZDOMJZGY . You are receiving this because you commented.Message ID: @.***>

salahhadjar commented 1 month ago

the chrome profiles are already created and already existe what am trying to do is to run like 4 real profiles and monitor them all for cloudflare , but the bot always monitor 1 profile only , 2024-10-04 16:04:35,939 - INFO - Navigating to https://nopecha.com/demo/cloudflare. 2024-10-04 16:04:36,024 - INFO - Navigating to https://nopecha.com/demo/cloudflare. 2024-10-04 16:04:36,024 - INFO - Navigating to https://nopecha.com/demo/cloudflare. 2024-10-04 16:04:36,027 - INFO - Navigating to https://nopecha.com/demo/cloudflare. 2024-4 16:04:36,024 - INFO - Navigating to https://nopecha.com/demo/cloudflare. 2024-10-04 16:04:36,027 - INFO - Navigating to https://nopecha.com/demo/cloudflare. 2024-10-04 16:04:49,685 - INFO - Cloudflare challenge detected on Profile 1. Attempting to solve... 2024-10-04 16:04:49,737 - INFO - Cloudflare challenge detected on Profile 2. Attempting to solve... 2024-10-04 16:04:49,785 - INFO - Cloudflare challenge detected on Profile 3. Attempting to solve... 2024-10-04 16:04:50,739 - INFO - Cloudflare challenge detected on Profile 4. Attempting to solve... 2024-10-04 16:04:54,374 - INFO - Successfully bypassed Cloudflare on Profile 3. 2024-10-04 16:04:54,375 - INFO - Successfully bypassed Cloudflare on Profile 4. 2024-10-04 16:04:54,381 - INFO - Successfully bypassed Cloudflare on Profile 1. 2024-10-04 16:04:54,382 - INFO - Successfully bypassed Cloudflare on Profile 2. and here is the code `import time import logging import threading import random import socket from CloudflareBypasser import CloudflareBypasser from DrissionPage import ChromiumPage, ChromiumOptions

def setup_logging(browser_index): log_file = f'cloudflare_bypassbrowser{browser_index}.log' logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', handlers=[ logging.StreamHandler(), logging.FileHandler(logfile, mode='w') ] ) return logging.getLogger(f'BrowserLogger{browser_index}')

def find_free_port(): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.bind(('', 0)) return s.getsockname()[1]

def get_chromium_options(browser_path: str, user_data_dir: str) -> ChromiumOptions: options = ChromiumOptions() options.set_paths(browser_path=browser_path) options.set_argument(f'--user-data-dir={user_data_dir}') port = find_free_port() options.set_argument(f'--remote-debugging-port={port}') options.set_argument('--no-sandbox') options.set_argument('--disable-dev-shm-usage') return options

def check_for_cloudflare(driver, logger, profile_name): while True: try: time.sleep(10)

        if "Just a moment..." in driver.html:
            logger.info(f"Cloudflare challenge detected on {profile_name}. Attempting to solve...")
            cf_bypasser = CloudflareBypasser(driver)
            cf_bypasser.bypass()
            logger.info(f"Successfully bypassed Cloudflare on {profile_name}.")
        else:
            logger.info(f"No Cloudflare challenge detected on {profile_name}.")

    except Exception as e:
        logger.error(f"Error while checking Cloudflare in {profile_name}: {str(e)}")
        time.sleep(5)

def bypass_cloudflare(url: str, browser_path: str, user_data_dir: str, browser_index: int): logger = setup_logging(browser_index) options = get_chromium_options(browser_path, user_data_dir)

driver = ChromiumPage(addr_or_opts=options)
try:
    logger.info(f'Navigating to {url}.')
    driver.get(url)

    check_for_cloudflare(driver, logger, f'Profile {browser_index}')

except Exception as e:
    logger.error("An error occurred: %s", str(e))
finally:
    logger.info('Monitoring completed for Profile %d. Browser remains open.', browser_index)

def main(): chrome_paths = [ r'C:\CF\GoogleChrome\New folder\CHROME 1\chrome.exe', r'C:\CF\GoogleChrome\New folder\CHROME 2\chrome.exe', r'C:\CF\GoogleChrome\New folder\CHROME 3\chrome.exe', r'C:\CF\GoogleChrome\New folder\CHROME 4\chrome.exe', ]

user_data_dirs = [
    r'C:\CF\GoogleChrome\New folder\CHROME 1\Data\profile',
    r'C:\CF\GoogleChrome\New folder\CHROME 2\Data\profile',
    r'C:\CF\GoogleChrome\New folder\CHROME 3\Data\profile',
    r'C:\CF\GoogleChrome\New folder\CHROME 4\Data\profile',
]

url = 'https://nopecha.com/demo/cloudflare'

threads = []
for i in range(len(chrome_paths)):
    chrome_path = chrome_paths[i]
    user_data_dir = user_data_dirs[i]
    thread = threading.Thread(target=bypass_cloudflare, args=(url, chrome_path, user_data_dir, i + 1))
    threads.append(thread)
    thread.start()

for thread in threads:
    thread.join()

if name == 'main': main() `