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

时间:2016-11-10 21:46:57

标签: android json jsoup wunderground

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

  

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

我需要播放的视频网址嵌入在html代码中,其网址如下:

  

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

有没有办法从json响应中获取该网址" camurl"?我已经听说过这个术语" html抓取",是否可以从json响应html页面获取嵌入式视频网址?

这就是网络摄像头的完整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并阅读了文档,但无法弄清楚如何获取所需的网址。以下是网址在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>

我怎样才能得到&#34; current.mp4&#34;来自html代码的url?

1 个答案:

答案 0 :(得分:1)

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

  1. Retrieve the html code with jsoup

    Document doc = Jsoup.connect("http://www.wunderground.com/webcams/cadot1/902/show.html").get();
    
  2. 然后,使用类 videoText

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

    这将为您提供条目列表。现在只需选择以current.mp4结尾的那个。

  3. 要检索current.mp4网址:

    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
    }