gpt4 book ai didi

java - 如何使用 Jsoup 从解析的 HTML 中获取变量的值

转载 作者:行者123 更新时间:2023-12-02 03:25:46 27 4
gpt4 key购买 nike

我正在使用 Jsoup 来获取 weather station 的 html 。当我在天气页面上右键单击该页面并检查它时,在 DOM 中我看到了我想要的元素,即

<span data-gm-wx-temperature="::todayWxcardVm.obs.temperature" data-text-to-replace="77"><!-- ngIf: tempPrefix --> <!-- ngIf: hasValue --><span data-ng-if="hasValue" class="dir-ltr" data-ng-bind="temp | safeDisplay">77</span><!-- end ngIf: hasValue --><!-- ngIf: hasValue --><sup data-ng-if="hasValue" class="deg dir-ltr">°</sup><!-- end ngIf: hasValue --><!-- ngIf: showTempUnit -->
<!-- ngIf: !hasValue --></span>

我希望能够获取 data-text-to-replace ="77"中的温度值,但我的问题是 Jsoup 返回给我:

<span data-gm-wx-temperature="::todayWxcardVm.obs.temperature" data-text-to-replace="{{ '[[ obs.temperature ]]'.indexOf('\[\[') !== -1 ? '--' : '[[ obs.temperature ]]' }}">[[ obs.temperature ]]</span>

其中,我得到的不是 data-text-to-replace=“77”,而是 data-text-to-replace={{ '[[ obs.Temperature ]] }}',我假设是 Angular 网站创建者用于存储温度值的 .js 变量。

我如何获得obs.温度的实际值?

最佳答案

您无法提取温度值的原因是它不是从 weather.comHTML 形式发送的。相反,它是通过使用通过 AJAX 调用 api.weather.com 获得的值在客户端填充的。

您可以直接从 Java 客户端调用此 Web 服务,但是我认为这不会构成对该服务的合法使用,因为我可以看到 API key 在服务输入中。

以下是使用 weather.com API key 的 Web 服务请求示例。

https://api.weather.com/v2/turbo/vt1precipitation;vt1currentdatetime;vt1pollenforecast;vt1dailyForecast;vt1observation?units=e&language=en-US&geocode=30.51,-97.68&format=json&apiKey=c1ea9f47f6a88b9acb43aba7faf389d4

如果我们说 outputJSON 响应,那么下面将为您提供温度。

output.vt1observation.feelsLike

下面是一个output.vt1observation示例。

{
"altimeter":29.86,
"barometerTrend":"Rising",
"barometerCode":1,
"barometerChange":0.01,
"dewPoint":74,
"feelsLike":77,
"gust":null,
"humidity":91,
"icon":29,
"observationTime":"2016-08-13T21:05:00-0500",
"obsQualifierCode":null,
"obsQualifierSeverity":null,
"phrase":"Partly Cloudy",
"precip24Hour":0.29,
"snowDepth":0.0,
"temperature":77,
"temperatureMaxSince7am":97,
"uvIndex":0,
"uvDescription":"Low",
"visibility":10.000,
"windSpeed":1,
"windDirCompass":"N",
"windDirDegrees":0
}

关于java - 如何使用 Jsoup 从解析的 HTML 中获取变量的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38938504/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com