如何使用 JSOUP 从 Android 的 Weather Underground 响应中获取网络摄像头视频文件 url?

How to get webcam video file url from Weather Underground response in Android using JSOUP?

我希望能够在视频视图中播放网络摄像头视频响应,但是 Weather Underground 对 "camurl" 的 json 响应是这样的:

http://www.wunderground.com/webcams/cadot1/902/show.html

我需要播放的视频的 url 嵌入在 html 代码中 url 如下所示:

https://www.wunderground.com/webcams/cadot1/1216/video.html?month=11&year=2016&filename=current.mp4

有没有办法从 json 响应 "camurl" 中获取 url?我听说过这个术语 "html scraping",是否可以从 json 响应 html 页面获取嵌入视频 url?

网络摄像头的 json 完整响应如下所示:

    {
    "handle": "mahouser",
    "camid": "mahouserCAM1",
    "camindex": "1",
    "assoc_station_id": "KCACAMAR18",
    "link": "http://",
    "linktext": "Michael Houser",
    "cameratype": "Foscam FI9900P",
    "organization": "",
    "neighborhood": "Camarillo Hills",
    "zip": "93010-12",
    "city": "CAMARILLO",
    "state": "CA",
    "country": "US",
    "tzname": "America/Los_Angeles",
    "lat": "34.24947357",
    "lon": "-119.03993988",
    "updated": "2016-11-10 20:57:24",
    "updated_epoch": "",
    "downloaded": "2016-11-08 20:38:48",
    "isrecent": "1",
    "CURRENTIMAGEURL": "http://icons.wunderground.com/webcamramdisk/m/a/mahouser/1/current.jpg?t=1478812080",
    "WIDGETCURRENTIMAGEURL": "http://icons.wunderground.com/webcamramdisk/m/a/mahouser/1/widget.jpg?t=1478812080",
    "CAMURL": "http://www.wunderground.com/webcams/mahouser/1/show.html"
}

我查看了 jsoup 并阅读了文档,但无法弄清楚如何获得所需的 url。这是 url 在 html 中的样子:

    <td class="day">
    <div class="row">
    <div class="small-2 medium-5 columns">
    <a href="/history/airport/KAJO/2016/11/15/DailyHistory.html" class="day-num">
    15
    </a>
    </div>
    <div class="small-10 medium-7 columns">
    <img src="//icons.wxug.com/i/c/v4/clear.svg" alt="Clear" class="right" />
    </div>
    </div>
    <div class="calThumb">
    <a href="http://icons.wunderground.com/webcamramdisk/c/a/cadot1/902/current.jpg?1479239986" rel="lightbox[webcam]" title="">
    <img src="http://icons.wunderground.com/webcamramdisk/c/a/cadot1/902/current-thumb.jpg?1479239986" width="100" height="75" alt="" title="Click to view the time-lapse video for this day." />
    </a>
    </div>
    <p><a href="video.html?month=11&year=2016&filename=current.mp4" class="videoText">View Video</a></p>
    </td>

如何从 html 代码中获取 "current.mp4" url?

有很多可能的方法,但这里有一个简单的解决方案:

  1. Retrieve the html code with jsoup:

    Document doc = Jsoup.connect("http://www.wunderground.com/webcams/cadot1/902/show.html").get();
    
  2. 然后,检索具有 class videoText:

    的所有元素
    Elements elements = doc.getElementsByClass("videoText");
    

    这将为您提供条目列表。现在只需 select 以 current.mp4.

  3. 结尾的那个
  4. 获取current.mp4URL:

    for (Element link : elements) {
        String linkHref = link.attr("href");
        // linkHref contains something like video.html?month=11&year=2016&filename=current.mp4
        // TODO check if linkHref ends with current.mp4
    }