使用SeleniumBase无法绕过Ahrefs免费DR服务的Cloudflare验证码
问题:SeleniumBase 访问 Ahrefs 时被 Cloudflare 验证码拦截
尝试用SeleniumBase的UC模式访问Ahrefs网站权威检测页面,获取DR、反向链接、链接网站数等数据,但始终被Cloudflare验证码拦截,验证码页面循环无法通过。
使用代码如下:
from seleniumbase import SB, Driver import time with SB(uc=True, test=True) as sb: url = "https://ahrefs.com/website-authority-checker" sb.driver.uc_open_with_reconnect(url, 3) sb.type('/html/body/div/div[1]/section[1]/div/div/div/div/div/div[2]/div[2]/div[2]/form/div/div/div[1]/div/input', 'https://www.peopleperhour.com') time.sleep(2) sb.click('/html/body/div/div[1]/section[1]/div/div/div/div/div/div[2]/div[2]/div[2]/form/div/button') time.sleep(10) sb.post_message("SeleniumBase wasn't detected", duration=4)
验证码界面截图:
可行解决方案
1. 优化SeleniumBase UC模式配置
默认UC模式可能扛不住Cloudflare最新检测,试试加这些参数:
with SB(uc=True, test=True, incognito=True, headless=False, agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36") as sb: url = "https://ahrefs.com/website-authority-checker" # 增加重连次数,给足验证时间 sb.driver.uc_open_with_reconnect(url, 5) # 等输入框加载完成再操作,别用硬等sleep sb.wait_for_element_visible('input[name="url"]', timeout=15) # 用属性定位替代绝对XPath,既稳又减少自动化特征 sb.type('input[name="url"]', 'https://www.peopleperhour.com') # 用回车提交代替点击按钮,更像真实用户操作 sb.press_key('input[name="url"]', 'Enter') sb.sleep(15)
- 开无痕模式避免缓存Cookie暴露痕迹
- 手动指定最新Chrome UA,防止被识别为自动化工具
- 用元素属性定位,避免页面结构变动导致定位失效
2. 模拟真实用户交互
Cloudflare会盯鼠标移动、滚动这些行为,加些模拟操作:
with SB(uc=True, test=True, incognito=True) as sb: sb.driver.uc_open_with_reconnect(url, 5) sb.wait_for_element_visible('input[name="url"]', timeout=15) # 模拟鼠标移到输入框 sb.move_to_element('input[name="url"]') sb.sleep(1) sb.type('input[name="url"]', 'https://www.peopleperhour.com') sb.sleep(2) # 模拟滚动页面 sb.scroll_to_bottom() sb.sleep(1) sb.scroll_to_top() sb.sleep(1) sb.click('button[type="submit"]') sb.sleep(15)
通过这些小动作,让操作更贴近真人,降低被检测概率。
3. 绑定本地已验证的浏览器会话
如果上面的方法都不行,试试用本地Chrome已经通过Cloudflare验证的会话:
options = { "user_data_dir": r"C:\Users\你的用户名\AppData\Local\Google\Chrome\User Data", "profile_directory": "Default" } with SB(uc=True, test=True, browser_args=options) as sb: sb.driver.get("https://ahrefs.com/website-authority-checker") # 本地浏览器已经通过验证的话,直接操作就行 sb.wait_for_element_visible('input[name="url"]', timeout=10) sb.type('input[name="url"]', 'https://www.peopleperhour.com') sb.click('button[type="submit"]') sb.sleep(10)
注意:用之前要关掉本地Chrome,避免进程冲突;路径换成你自己的Chrome用户数据目录。
4. 直接用Ahrefs官方API(最靠谱)
绕验证码长期来看不稳定,Ahrefs有官方API,直接调用就能拿数据:
import requests API_KEY = "你的API密钥" target_url = "https://www.peopleperhour.com" # 按需指定要获取的指标 endpoint = f"https://apiv2.ahrefs.com?token={API_KEY}&target={target_url}&mode=domain&output=json&metrics=domain_rating,backlinks,referring_domains" response = requests.get(endpoint) data = response.json() print(f"DR: {data['domain_rating']}") print(f"反向链接数: {data['backlinks']}") print(f"链接网站数: {data['referring_domains']}")
虽然需要付费订阅,但能稳定拿数据,彻底避开验证码问题。
内容的提问来源于stack exchange,提问作者Muhammad Anas Raza
相关产品推荐
相关产品推荐

