Jsoup 只从开始获取 HTML 的几行,甚至不到 25%

Jsoup only fetches few lines of the HTML from start which is not even 25%

当我尝试在网站上执行 CTRL+U 时,它也比我从 jsoup 获得的更多。我使用的站点是 Open SAP -> https://open.sap.com/courses 已尝试超时和 maxbodysize 以及 jsoup.connect。 现在我的代码如下所示:

private static String getHtml(String location) throws IOException {
    URL url = new URL(location);
    URLConnection conn = url.openConnection();
    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String input;
    StringBuilder builder = new StringBuilder();
    while((input = in.readLine()) != null)
    {
         builder.append(input);
    }

    return builder.toString();
}
document = Jsoup.parse(getHtml(URL));

但仍然返回相同的 HTML。通过 selenium 它是可能的,但它有点慢,所以还有其他方法可以实现这一目标吗? 因为目的是找出课程的链接,然后加载它们以找到他们的课程摘要,用 selenium 会太慢。

请建议在这里可以做什么。

本页面的页面内容是基于js在您的浏览器中构建的。您需要一个支持 js 的框架来执行此操作。

使用 HtmlUnit 我得到了这样的页面

String url = "https://open.sap.com/courses";

try (final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_68)) {
    webClient.getOptions().setThrowExceptionOnScriptError(false);

    HtmlPage page = webClient.getPage(url);
    webClient.waitForBackgroundJavaScriptStartingBefore(10_000);

    System.out.println("-------------------------------");
    System.out.println(page.asText());
    System.out.println("-------------------------------");
}

HtmlUnit 具有丰富的 API 功能,可以对页面对象执行任何您喜欢的操作,例如搜索 controls/content、单击控件或从页面的某些部分提取文本。