cloudflare_bypass_for_scraping.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import time
  2. from DrissionPage import ChromiumPage
  3. class CloudflareBypasser:
  4. def __init__(self, driver: ChromiumPage, log=False, logger=None):
  5. self.driver = driver
  6. self.log = log
  7. self.logger = logger
  8. def log_message(self, message):
  9. if self.log:
  10. if self.logger:
  11. self.logger(f"[CF-Bypass] {message}")
  12. def search_recursively_shadow_root_with_iframe(self, ele, depth=0, max_depth=3):
  13. if depth > max_depth:
  14. return None
  15. try:
  16. if ele.shadow_root:
  17. child = ele.shadow_root.child()
  18. if child and child.tag == "iframe":
  19. return child
  20. else:
  21. for child in ele.children():
  22. result = self.search_recursively_shadow_root_with_iframe(child, depth + 1, max_depth)
  23. if result:
  24. return result
  25. except Exception:
  26. pass
  27. return None
  28. def search_recursively_shadow_root_with_cf_input(self, ele):
  29. if ele.shadow_root:
  30. if ele.shadow_root.ele("tag:input"):
  31. return ele.shadow_root.ele("tag:input")
  32. else:
  33. for child in ele.children():
  34. result = self.search_recursively_shadow_root_with_cf_input(child)
  35. if result:
  36. return result
  37. return None
  38. def locate_cf_button(self, dfs=False):
  39. try:
  40. button = None
  41. eles = self.driver.eles("tag:input")
  42. for ele in eles:
  43. attrs = ele.attrs
  44. if "name" in attrs and "type" in attrs:
  45. if "turnstile" in attrs["name"] and attrs["type"] == "hidden":
  46. if ele.parent() and ele.parent().shadow_root:
  47. button = ele.parent().shadow_root.child()("tag:body").shadow_root("tag:input")
  48. break
  49. if button:
  50. return button
  51. else:
  52. if dfs:
  53. ele = self.driver.ele("tag:body")
  54. iframe = self.search_recursively_shadow_root_with_iframe(ele)
  55. if iframe:
  56. return self.search_recursively_shadow_root_with_cf_input(iframe("tag:body"))
  57. return None
  58. except Exception as e:
  59. if self.log:
  60. self.log_message(f"Error locating verification button: {e}")
  61. return None
  62. def click_verification_button(self, is_dfs):
  63. try:
  64. button = self.locate_cf_button(dfs=is_dfs)
  65. if button:
  66. self.log_message("Verification button found. Attempting to click.")
  67. if button.states.is_displayed and button.states.is_enabled:
  68. button.click()
  69. time.sleep(1)
  70. else:
  71. self.log_message("Button is not clickable.")
  72. except Exception as e:
  73. if self.log:
  74. self.log_message(f"Error clicking verification button: {e}")
  75. def is_bypassed(self):
  76. try:
  77. title = self.driver.title.lower()
  78. return "just a moment" not in title and "请稍候" not in title
  79. except Exception as e:
  80. if self.log:
  81. self.log_message(f"Error checking page title: {e}")
  82. return False
  83. def bypass(self, max_retry=5):
  84. for i in range(max_retry):
  85. if self.is_bypassed():
  86. return True
  87. self.log_message(f"Verification page detected. Trying to bypass times={i}...")
  88. self.click_verification_button(False)
  89. time.sleep(2)
  90. return self.is_bypassed()
  91. def handle_waiting_room(self):
  92. wait_start = time.time()
  93. while True:
  94. try:
  95. html = self.driver.html.lower()
  96. if "cloudflare" not in html:
  97. break
  98. if "file d'attente" in html or "waitting room" in html:
  99. if time.time() - wait_start > 60 * 60:
  100. self.log_message("Waiting room timeout (1h).")
  101. break
  102. self.log_message("In Waiting Room... Waiting for auto-refresh.")
  103. time.sleep(10)
  104. else:
  105. break
  106. except Exception as e:
  107. if self.log:
  108. self.log_message(f"Error checking html: {e}")