This commit is contained in:
2025-11-21 01:56:01 +08:00
parent 2e2cd3e260
commit f3b4ec2601
2 changed files with 38 additions and 26 deletions

View File

@@ -1,3 +1,5 @@
# 0.1.0
- 修复cloudflare验证问题
# 0.0.9 # 0.0.9
- 修正cloudflare验证 - 修正cloudflare验证
# 0.0.8 # 0.0.8

View File

@@ -10,6 +10,7 @@ from mail_ import mail_
from bit_browser import bit_browser from bit_browser import bit_browser
from api import api from api import api
class Auto: class Auto:
def __init__(self, http: str = None): def __init__(self, http: str = None):
self.browser = Chromium(http) self.browser = Chromium(http)
@@ -17,9 +18,9 @@ class Auto:
pass pass
# cf打码 # cf打码
def solve_cloudflare(self): def solve_cloudflare(self, is_ok: bool = False):
tab = self.browser.latest_tab tab = self.browser.latest_tab
for _ in range(5): for _ in range(3):
self.tab.wait(1) self.tab.wait(1)
try: try:
shadow1 = tab.ele( shadow1 = tab.ele(
@@ -31,16 +32,16 @@ class Auto:
if shadow2: if shadow2:
logger.debug("找到Cloudflare iframe body shadow root") logger.debug("找到Cloudflare iframe body shadow root")
status = shadow2.ele( status = shadow2.ele(
'x://span[text()="Verifying..."]', timeout=1) 'x://span[text()="Verifying..."]', timeout=1.5)
if status: if status:
tab.wait(3) tab.wait(3)
status = shadow2.ele( status = shadow2.ele(
'x://span[text()="Success!"]', timeout=1) 'x://span[text()="Success!"]', timeout=1.5)
if status: if status:
logger.debug("Cloudflare验证成功") logger.debug("Cloudflare验证成功")
return True return True
checkbox = shadow2.ele( checkbox = shadow2.ele(
'x://input[@type="checkbox"]', timeout=1) 'x://input[@type="checkbox"]', timeout=1.5)
if checkbox: if checkbox:
checkbox.click() checkbox.click()
logger.debug("点击Cloudflare复选框") logger.debug("点击Cloudflare复选框")
@@ -49,8 +50,10 @@ class Auto:
# return False # return False
except Exception as e: except Exception as e:
# logger.error(f"处理Cloudflare异常: {e}") # logger.error(f"处理Cloudflare异常: {e}")
if is_ok:
logger.debug(f"cloudflare处理通过: {e}") logger.debug(f"cloudflare处理通过: {e}")
return True return True
return self.solve_cloudflare(is_ok=True)
tab.wait(1) tab.wait(1)
return False return False
@@ -75,11 +78,13 @@ class Auto:
else: else:
logger.debug("Cloudflare验证成功.") logger.debug("Cloudflare验证成功.")
self.tab.wait(1.5) self.tab.wait(1.5)
bol = self.tab.ele('t:h1@text():Sorry, you have been blocked', timeout=1) bol = self.tab.ele(
't:h1@text():Sorry, you have been blocked', timeout=1)
if bol: if bol:
logger.debug("ip被ban秒") logger.debug("ip被ban秒")
return False return False
bol = self.tab.ele('t:div@text():ERR_SSL_PROTOCOL_ERROR', timeout=1) bol = self.tab.ele(
't:div@text():ERR_SSL_PROTOCOL_ERROR', timeout=1)
if bol: if bol:
logger.debug("刷新网页") logger.debug("刷新网页")
self.tab.refresh() self.tab.refresh()
@@ -125,7 +130,8 @@ class Auto:
logger.debug("异常界面") logger.debug("异常界面")
self.tab.wait(1) self.tab.wait(1)
return self.click_continue(bl=True) return self.click_continue(bl=True)
bol = self.tab.ele('t:h2@text()=You are being rate limited', timeout=1) bol = self.tab.ele(
't:h2@text()=You are being rate limited', timeout=1)
if bol: if bol:
logger.debug("被限流, 退出") logger.debug("被限流, 退出")
return False return False
@@ -166,7 +172,6 @@ class Auto:
province = random.choice(list(cities.keys())) province = random.choice(list(cities.keys()))
return province, random.choice(cities.get(province, [])) return province, random.choice(cities.get(province, []))
def get_province_by_city(self, city: str) -> str | None: def get_province_by_city(self, city: str) -> str | None:
""" """
根据城市名称解析对应省份 根据城市名称解析对应省份
@@ -186,7 +191,6 @@ class Auto:
} }
return mapping.get(city) return mapping.get(city)
# 随机实物 # 随机实物
def get_random_food(self, city: str, shop: str) -> list[str]: def get_random_food(self, city: str, shop: str) -> list[str]:
""" """
@@ -395,6 +399,8 @@ def get_proxy( city: str):
"""指纹浏览器操作""" """指纹浏览器操作"""
# 创建指纹浏览器 # 创建指纹浏览器
def create_fingerprint_browser(city: str): def create_fingerprint_browser(city: str):
""" """
根据城市创建指纹浏览器并执行问卷流程 根据城市创建指纹浏览器并执行问卷流程
@@ -448,6 +454,7 @@ def create_fingerprint_browser(city: str):
except Exception as e: except Exception as e:
logger.error(f"{city} 删除浏览器异常: {e}") logger.error(f"{city} 删除浏览器异常: {e}")
def run_city_forever(city: str): def run_city_forever(city: str):
""" """
持续循环运行指定城市流程:完成一次即关闭并删除浏览器,然后重新创建继续运行 持续循环运行指定城市流程:完成一次即关闭并删除浏览器,然后重新创建继续运行
@@ -462,6 +469,7 @@ def run_city_forever(city: str):
logger.error(f"{city} 流程异常: {e}") logger.error(f"{city} 流程异常: {e}")
time.sleep(2) time.sleep(2)
def run_all_cities_concurrently(): def run_all_cities_concurrently():
""" """
多线程并发运行所有城市流程 多线程并发运行所有城市流程
@@ -471,7 +479,8 @@ def run_all_cities_concurrently():
# cities = ['Calgary'] # cities = ['Calgary']
threads = [] threads = []
for city in cities: for city in cities:
t = threading.Thread(target=run_city_forever, args=(city,), name=f"{city}-thread") t = threading.Thread(target=run_city_forever,
args=(city,), name=f"{city}-thread")
t.start() t.start()
threads.append(t) threads.append(t)
logger.info(f"{city} 线程已启动") logger.info(f"{city} 线程已启动")
@@ -480,6 +489,7 @@ def run_all_cities_concurrently():
t.join() t.join()
logger.info("所有城市流程执行完成") logger.info("所有城市流程执行完成")
if __name__ == "__main__": if __name__ == "__main__":
# auto = Auto() # auto = Auto()
# auto.get_random_food('a') # auto.get_random_food('a')