java - 需要 HtmlUnit 绕过 cloudflare DDOS 和 cookie
问题描述
我正在尝试抓取一个使用 Cloudflare 的页面,直到最近这才可能没有问题。但是截至昨天,我遇到了 503(ddos 保护页面)。而今天它转变为简单的 403。检查响应我可以看到页面正在请求我启用 cookie。我目前正在使用 HtmlUnit 执行刮擦,并且将 BrowserVersion 设置为 Chrome。
这是我目前的尝试:
private HtmlPage scrapeJS(String targetUrl) throws ScrapeException {
Log.verbose("Attempting JS scrape ...");
WebClient client = new WebClient(BrowserVersion.CHROME);
client.getOptions().setJavaScriptEnabled(true);
client.getOptions().setCssEnabled(css);
client.getOptions().setUseInsecureSSL(insecureSSL);
client.setCookieManager(new CookieManager());
client.getOptions().setRedirectEnabled(true);
HtmlPage page;
try {
page = client.getPage(targetUrl);
client.waitForBackgroundJavaScript(10000);
} catch (FailingHttpStatusCodeException e){
Log.verbose("JS scrape resulted in " + e.getStatusCode());
throw new ScrapeException(source, e);
} catch (IOException e){
throw new ScrapeException(source, e);
}
return page;
}
我应该提到,这在我的桌面上的 cookie 检查和 503 都失败了,但它没有通过我的笔记本电脑(这是一个 mac)上的 cookie 检查。
我环顾四周,但大多数处理 HtmlUnit 的帖子似乎有点过时,并且解决方案(例如等待后台 JS)不起作用,也无法在 firefox 和 chrome 之间更改用户代理。
解决方案
我在这里解决了https://stackoverflow.com/a/69760898/2751894
只需使用以下 jvm 属性之一:
-Djdk.tls.client.protocols="TLSv1.3,TLSv1.2" 或 -Dhttps.protocols="TLSv1.3,TLSv1.2"
推荐阅读
- pandas - 使用 pandas 创建一个新列以获取不同货币的卢比换算值
- javascript - 播放 blob 视频文件
- ocaml - 如何使用一些默认实现在 OCaml 中实现模块签名?
- javascript - 如何从 chome.tabs.captureVisibleTab 获取屏幕截图
- angular - Material datePicker(Angular)中的多个日期选择
- jenkins - Jenkins Pipeline-如何使用发布插件?
- c# - 在 C# 中使用字符串参数调用 Fortran DLL
- amazon-web-services - AWS Lambda NodeJS 启动 EMR Spark 问题
- c++ - UE4:通过 C++ 或蓝图添加时组件的不同细节面板
- spring - 如何立即停止 Spring Cloud Data Flow 中的作业