在 java 中使用 HtmlUnit 解析动态网页无效

Parsing of a dynamic web page using HtmlUnit in java is not working

Image explaining the data to be extracted

我正在尝试从 java 的 web page (marked red in the image) using HtmlUnit 库中提取数据。但是我无法得到那个特定的值。

WebClient webClient = new WebClient(BrowserVersion.CHROME);
Thread.sleep(5000);
HtmlPage page = webClient.getPage("https://earth.nullschool.net/#current/wind/isobaric/500hPa/orthographic=-283.71,14.19,2183/loc=76.850,11.440");
Thread.sleep(5000);
System.out.println(page.asXml());

我检查了我在控制台 window 上获得的 html。它不包含值。

<p>
  <span id="location-wind" class="location">
          </span>
  <span id="location-wind-units" class="location text-button">
          </span>
</p>

因为这些是通过Java脚本填写的。当您加载页面时,这些字段最初是空的。您可以通过查看源代码并搜索 id="location.

来检查这一点

页面发出两个额外的 HTTP 请求来获取动态数据:

  1. https://earth.nullschool.net/data/earth-topo.json?v3
  2. https://gaia.nullschool.net/data/gfs/current/current-wind-isobaric-500hPa-gfs-0.5.epak

此数据中的某处(加起来大约 1.2 MB)是您要查找的数据。最好的办法是使用工具(也许是在线工具)将 JSON 转换为 Java 对象,或者研究 JSON 并编写代码以获取您需要的特定数据之后。

也就是说,如果该数据在 JSON 中,我不相信。 EPAK 文件似乎是某种带有嵌入式 JSON 的二进制数据,但我无法确定数据是否可能在其中。

另一种方法是使用 Selenium,让它为您解析页面,并从那里检索数据。