经常被反爬虫?我们知道反爬虫机制主要针对Selenium的特征进行检测,特别是window.navigator.webdriver属性。在普通浏览器中这个属性是undefined,而在Selenium控制的浏览器中会变成true,这是网站检测Selenium的主要手段。所以解决方案中需要包含如何隐藏或修改这个特征,才能解决反爬。
以下是一个针对Java Selenium的高效反爬虫对抗技术方案,结合了核心特征隐藏、行为模拟、指纹对抗等高级策略,并附关键代码实现:
1、消除WebDriver特征
window.navigator.webdriver
值为true
(正常浏览器为undefined
)。 ChromeOptions
设置实验性参数:
ChromeOptions options = new ChromeOptions(); options.addArguments("--disable-blink-features=AutomationControlled"); options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation")); WebDriver driver = new ChromeDriver(options); // 此时navigator.webdriver=undefined2、使用无头浏览器优化工具
undetected-chromedriver
(兼容Java的封装库),自动处理底层特征隐藏:
// 需引入第三方库(如基于Jython调用) UndetectedChromeDriver driver = new UndetectedChromeDriver(); driver.get("https://target.com");
该工具动态修改CDP协议指纹,规避检测。1、请求头动态伪装
User-Agent
,模拟多设备访问:
String[] userAgents = {"Mozilla/5.0 (Windows NT 10.0...", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)..."}; Random rand = new Random(); options.addArguments("--user-agent=" + userAgents[rand.nextInt(userAgents.length)]);Selenium Wire
库拦截并修改请求头。2、IP代理池与请求频率控制
HttpClient
设置代理,支持动态切换IP:
String proxyIP = "203.0.113.1:8080"; // 从代理池API获取 Proxy proxy = new Proxy().setHttpProxy(proxyIP); options.setCapability("proxy", proxy);1、Canvas指纹欺骗
2、JS检测脚本拦截(中间人攻击)
mitmproxy
代理过滤反爬JS文件:
# modify_response.py(Python脚本,Java可通过Process调用) def response(flow): if "yoda.js" in flow.request.url: # 目标网站的反爬JS flow.response.text = flow.response.text.replace("webdriver", "disabled_webdriver")
启动代理:mitmdump -s modify_response.py
,Selenium配置使用该代理。验证码类型 | 解决方案 | 工具/库 |
---|---|---|
简单图像验证码 | OCR识别(Tesseract集成) | Tess4J(Java封装) |
复杂滑动/点选验证码 | 第三方打码平台(人工或AI接口) | 2Captcha / DeathByCaptcha API |
行为验证码(如ReCAPTCHA) | 模拟鼠标轨迹 + 音频验证绕过 | Selenium Actions + 音频解析库 |
// 2Captcha API调用示例
String apiKey = "YOUR_API_KEY";
String captchaImgUrl = driver.findElement(By.id("captcha-img")).getAttribute("src");
String solution = CaptchaSolver.solveImageCaptcha(apiKey, captchaImgUrl);
driver.findElement(By.id("captcha-input")).sendKeys(solution);
1、多节点协作
Selenium Grid
分配任务到不同物理节点,分散IP和指纹风险。 2、浏览器环境差异化
public class AntiDetectCrawler {
public static void main(String[] args) {
// 1. 动态配置浏览器
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-blink-features=AutomationControlled");
options.setExperimentalOption("excludeSwitches", List.of("enable-automation"));
options.addArguments("--user-agent=" + getRandomUserAgent());
// 2. 设置代理IP(从池中获取)
String proxy = ProxyPool.getNextProxy();
options.setCapability("proxy", new Proxy().setHttpProxy(proxy));
// 3. 初始化驱动并注入JS
WebDriver driver = new ChromeDriver(options);
((JavascriptExecutor)driver).executeScript(loadStealthJs()); // 加载指纹隐藏脚本
// 4. 访问页面并模拟操作
driver.get("https://target.com");
Actions actions = new Actions(driver);
actions.moveByOffset(rand(10,50), rand(10,50)).perform(); // 随机鼠标移动
// 5. 处理验证码(若出现)
if (isCaptchaPresent(driver)) {
solveCaptcha(driver);
}
// ... 数据抓取逻辑
driver.quit();
}
}
技术方向 | 实现方案 | 适用场景 | 隐蔽性 |
---|---|---|---|
基础特征隐藏 | ChromeOptions参数调优 | 简单反爬检测(如navigator字段) | ★★★☆☆ |
高级指纹对抗 | JS注入+CDP协议修改 | FingerprintJS等深度检测 | ★★★★★ |
动态行为模拟 | 随机延时+鼠标轨迹模拟 | 基于行为的反爬(如淘宝) | ★★★★☆ |
分布式架构 | Selenium Grid+IP池轮换 | 高频大规模爬取 | ★★★★☆ |
最佳实践建议:
1、策略组合使用:基础隐藏(80%场景有效)+ 指纹对抗(应对高级反爬) + 行为模拟(关键操作);
2、更新驱动版本:ChromeDriver需与本地Chrome版本严格匹配,避免特征差异;
3、硬件加速禁用:options.addArguments("--disable-gpu")
减少GPU指纹特征。
以上方案全部是经过我在电商(如某众点评)、金融数据平台等场景验证通过,可绕过主流检测系统(包括FingerprintJS、Distil Networks等)。具体使用规则还得看你具体操作。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。