forked from sarperavci/CloudflareBypassForScraping
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clear version with optimization
- Loading branch information
Anon-sys69
authored
Aug 11, 2024
1 parent
71666d9
commit ca37502
Showing
1 changed file
with
34 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,47 @@ | ||
import time | ||
from DrissionPage import ChromiumPage | ||
|
||
|
||
# This code is written for readibility and simplicity. It is not optimized for performance or real-world usage. | ||
# You can optimize the code by removing the unnecessary sleeps and checks. | ||
|
||
class CloudflareBypasser: | ||
def __init__(self, driver: ChromiumPage, max_retries=-1, log=True): | ||
self.driver = driver | ||
self.max_retries = max_retries | ||
self.log = log | ||
self.driver = driver | ||
|
||
def clickCycle(self): | ||
#reach the captcha button and click it | ||
# if iframe does not exist, it means the page is already bypassed. | ||
if self.driver.wait.ele_displayed('.spacer', timeout=1.5): | ||
time.sleep(1.5) | ||
self.driver.ele(".spacer", timeout=2.5).click() | ||
# The location of the button may vary time to time. I sometimes check the button's location and update the code. | ||
def log_message(self, message): | ||
if self.log: | ||
print(message) | ||
|
||
def click_verification_button(self): | ||
try: | ||
if self.driver.wait.ele_displayed('.spacer', timeout=1.5): | ||
self.log_message("Verification button found. Attempting to click.") | ||
self.driver.ele(".spacer", timeout=2.5).click() | ||
except Exception as e: | ||
self.log_message(f"Error clicking verification button: {e}") | ||
|
||
def isBypassed(self): | ||
title = self.driver.title.lower() | ||
# If the title does not contain "just a moment", it means the page is bypassed. | ||
# This is a simple check, you can implement more complex checks. | ||
return "just a moment" not in title | ||
def is_bypassed(self): | ||
try: | ||
title = self.driver.title.lower() | ||
return "just a moment" not in title | ||
except Exception as e: | ||
self.log_message(f"Error checking page title: {e}") | ||
return False | ||
|
||
def bypass(self): | ||
count = 0 | ||
while not self.isBypassed(): | ||
if 0 < self.max_retries + 1 <= count: | ||
if self.log: | ||
print("Exceeded maximum tries") | ||
try_count = 0 | ||
|
||
while not self.is_bypassed(): | ||
if 0 < self.max_retries + 1 <= try_count: | ||
self.log_message("Exceeded maximum retries. Bypass failed.") | ||
break | ||
|
||
self.log_message(f"Attempt {try_count + 1}: Verification page detected. Trying to bypass...") | ||
self.click_verification_button() | ||
|
||
try_count += 1 | ||
time.sleep(2) | ||
# A click may be enough to bypass the captcha, if your IP is clean. | ||
# I haven't seen a captcha that requires more than 3 clicks. | ||
if self.log: | ||
print("Verification page detected. Trying to bypass...") | ||
time.sleep(2) | ||
self.clickCycle() | ||
count += 1 | ||
|
||
if self.is_bypassed(): | ||
self.log_message("Bypass successful.") | ||
else: | ||
self.log_message("Bypass failed.") |