2016-11-10 114 views
0

我希望能夠發揮在視頻查看網絡攝像頭視頻響應但是從天氣地下的「camurl」 JSON響應就像這樣:如何使用JSOUP從Android中的Weather Underground響應中獲取網絡攝像頭視頻文件url?

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」獲取該網址?我聽說過這個術語「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> 

我怎樣才能像「current.mp4」 URL從HTML代碼中?

+0

是的,有可能使用HTML刮板。 – BlackHatSamurai

+0

這裏沒有JSoup代碼。請添加您的嘗試。 –

回答

1

有很多可能的方式,但這裏是一個簡單的解決辦法:

  1. Retrieve the html code with jsoup

    Document doc = Jsoup.connect("http://www.wunderground.com/webcams/cadot1/902/show.html").get(); 
    
  2. 然後,檢索與類圖文所有元素:

    Elements elements = doc.getElementsByClass("videoText"); 
    

    This w生病給你一個條目列表。現在只需選擇以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 
    } 
    
+0

這可以從單個html文件中獲得所有「videoText」元素,但是當需要針對特定​​文件鏈接刮取html鏈接數組時,該怎麼辦?即current.mp4。 JSOUP能夠快速檢索由json數組返回的每個html鏈接所需的mp4文件鏈接嗎? –

+0

您回答了我上面的問題,非常感謝。我會將此標記爲答案,但由於此答案而遇到了另一個問題。我應該注意。從這個方法得到的視頻網址就是這樣一個url。我假設我會獲得視頻的鏈接,以便我可以在應用內進行流式傳輸。 –

相關問題