首页 > 解决方案 > 需要 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 之间更改用户代理。

标签: javaweb-scrapinghtmlunit

解决方案


我在这里解决了https://stackoverflow.com/a/69760898/2751894

只需使用以下 jvm 属性之一:

-Djdk.tls.client.protocols="TLSv1.3,TLSv1.2" 或 -Dhttps.protocols="TLSv1.3,TLSv1.2"


推荐阅读