Java中如何绕过Cloudflare?求类似Python cloudscraper的解决方案
Java绕过Cloudflare 403错误的替代方案
你当前用Jsoup直接请求部署了Cloudflare的网站时遇到403,是因为Cloudflare检测到请求并非来自真实浏览器。以下是几种类似Python cloudscraper的Java解决方案:
1. 使用Undetected Chromedriver(结合Selenium)
这是稳定性最高的方案,通过模拟真实浏览器环境绕过Cloudflare的人机检测,支持处理Turnstile验证。
依赖配置(Maven)
<dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.15.0</version> </dependency> <dependency> <groupId>io.github.bonigarcia</groupId> <artifactId>webdrivermanager</artifactId> <version>5.6.0</version> </dependency> <dependency> <groupId>com.github.automatedowl</groupId> <artifactId>undetected-chromedriver</artifactId> <version>3.1.1</version> </dependency>
适配需求的示例代码
import io.github.bonigarcia.wdm.WebDriverManager; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import java.io.File; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.text.SimpleDateFormat; import java.util.Date; public Path checkCatalog() { WebDriver driver = null; try { // 配置浏览器驱动 WebDriverManager.chromedriver().setup(); ChromeOptions options = new ChromeOptions(); options.addArguments("--headless=new"); // 无头模式,需可视化可删除 options.addArguments("--disable-blink-features=AutomationControlled"); driver = new ChromeDriver(options); // 打开登录页,等待Cloudflare验证完成 driver.get(URL); Thread.sleep(5000); // 等待验证加载,可根据实际调整时长 // 执行登录操作 driver.findElement(By.name("email")).sendKeys("mail"); driver.findElement(By.name("password")).sendKeys("password"); driver.findElement(By.cssSelector("button[type='submit']")).click(); Thread.sleep(3000); // 等待登录跳转 // 访问下载页面 driver.get(DOWNLOADS_URL); Thread.sleep(3000); // 创建下载目录 String date = new SimpleDateFormat("yyyyMMdd").format(new Date()); Path downloadDir = Paths.get(DOWNLOADS + "/" + date); downloadDir.toFile().mkdirs(); // 触发下载(需根据页面实际下载按钮定位调整) driver.findElement(By.cssSelector("a.download-link")).click(); Thread.sleep(10000); // 等待下载完成 // 找到最新下载文件并重命名 File downloadedFile = findLatestFile(downloadDir.toFile()); Path targetPath = downloadDir.resolve("catalog.xlsx"); Files.move(downloadedFile.toPath(), targetPath); return targetPath; } catch (Exception e) { e.printStackTrace(); } finally { if (driver != null) { driver.quit(); } } return null; } // 辅助方法:获取目录下最新文件 private File findLatestFile(File dir) { File[] files = dir.listFiles(); if (files == null || files.length == 0) return null; File latest = files[0]; for (File file : files) { if (file.lastModified() > latest.lastModified()) { latest = file; } } return latest; }
2. 使用CloudflareSolverJava
这是专门针对Cloudflare的Java库,支持自动处理验证挑战,可与Jsoup或HttpClient结合使用。
依赖配置(Maven)
<dependency> <groupId>com.github.kklisura.cdt</groupId> <artifactId>cdt-java-client</artifactId> <version>1.3.0</version> </dependency> <dependency> <groupId>me.grapebaba</groupId> <artifactId>CloudflareSolverJava</artifactId> <version>1.0.2</version> </dependency>
适配现有代码的示例
import me.grapebaba.cloudflare_solver.CloudflareSolver; import org.jsoup.Connection; import org.jsoup.Jsoup; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Map; public Path checkCatalog() { try { // 通过Cloudflare验证并获取有效cookies CloudflareSolver solver = new CloudflareSolver(); Map<String, String> cookies = solver.solve(URL); // 执行登录并更新cookies Connection.Response loginResponse = Jsoup.connect(URL) .cookies(cookies) .data("email", "mail") .data("password", "password") .post() .connection() .execute(); cookies.putAll(loginResponse.cookies()); // 下载文件 Connection.Response downloadResponse = Jsoup.connect(DOWNLOADS_URL) .cookies(cookies) .maxBodySize(0) .ignoreContentType(true) .execute(); String date = new SimpleDateFormat("yyyyMMdd").format(new Date()); Path downloadDir = Paths.get(DOWNLOADS + "/" + date); downloadDir.toFile().mkdirs(); Path path = downloadDir.resolve("catalog.xlsx"); Files.write(path, downloadResponse.bodyAsBytes()); return path; } catch (Exception e) { e.printStackTrace(); } return null; }
注意事项
- Cloudflare的防护规则会频繁更新,上述工具需跟进版本才能保持有效性。
- 务必遵守目标网站的使用条款与
robots.txt协议,禁止恶意爬取或高频请求。
内容的提问来源于stack exchange,提问作者Nero
相关产品推荐
相关产品推荐

