2017-04-09 91 views
1

我需要從http://www.siriusxm.com/siriusxmhits1中拉出當前播放的藝術家和歌曲。我可以通過Internet Explorer瀏覽到這個網站,但這需要很長時間,所以我嘗試使用WINHTTP.WinHTTPRequest.5.1MSXML2.serverXMLHTTP,但都沒有提取我正在尋找的特定數據。我覺得我很接近,但是錯過了一些東西。使用來自siriusxm.com的XHR進行網頁掃描

下面是HTML片段:

<div id="on-the-air-content" style="display: block;"> 
    <div class="module-content theme-color-content-bg clearfix"> 
     <div id="onair-pdt" style="display: block;"> 
      <img alt="" src="//www.siriusxm.com/albumart/Live/2000/chainsmokers_58C328AC_t.jpg"> 
      <p class="onair-pdt-artist">Chainsmokers/Coldplay</p> 
      <p class="onair-pdt-song">Something Just Like This</p> 
     </div> 
     ... 
    </div> 
    ... 
</div> 

這裏是我當前的代碼:

Sub GetData() 

    Dim getArtist As Object 
    Dim getSong As Object 

    Set xmHtml = New HTMLDocument 
    With CreateObject("WINHTTP.WinHTTPRequest.5.1") 
     .Open "GET", "http://www.siriusxm.com/siriusxmhits1", False 
     .send 
     xmHtml.body.innerHTML = .responseText 
    End With 
    Set getArtist = xmHtml.getElementById("onair-pdt").getElementsByTagName("p")(0) 
    MsgBox (getArtist.innerText) 
    Set getSong = xmHtml.getElementById("onair-pdt").getElementsByTagName("p")(1) 
    MsgBox (getSong.innerText) 

End Sub 

如果我啓動Internet Explorer中,將工作使用下面的代碼,但時間太長了什麼我需要做的:

Sub GetData() 

    Dim DivID As HTMLObjectElement 
    Dim getArtist As Variant 
    Dim getSong As Variant 

    URL = "http://www.siriusxm.com/siriusxmhits1" 
    With IExplore 
     .Navigate URL 
     .Visible = False 
     Do While .readyState <> 4: DoEvents: Loop 
     Set doc = .document 
     Set DivID = doc.getElementById("onair-pdt") 
     getArtist = DivID.getElementsByClassName("onair-pdt-artist")(0).innerText 
     getSong = doc.getElementsByClassName("onair-pdt-song")(0).innerText 
    End With 

End Sub 

回答

0

網站http://www.siriusxm.com有一種A PI可用。我瀏覽Chrome瀏覽器中的鏈接http://www.siriusxm.com/hits1,然後打開開發工具窗口(F12),網絡選項卡,並檢查列表中的XHR。當前歌曲信息可以被檢索e。 G。通過以下步驟:

下面是示例顯示JSON響應結構,我使用的在線工具http://jsonviewer.stack.hu

JSON response

您可能如上所述,使用下面的VBA代碼來檢索信息。 JSON.bas模塊導入JSON處理的VBA項目。

Option Explicit 

Sub Test_siriusxm_com() 

    Dim s As String 
    Dim d As Date 
    Dim sUrl As String 
    Dim vJSON As Variant 
    Dim sState As String 
    Dim sArtists As String 
    Dim sComposer As String 
    Dim sAlbum As String 
    Dim sSong As String 

    ' Retrieve timestamp 
    With CreateObject("MSXML2.XMLHTTP") 
     .Open "GET", "http://www.siriusxm.com/sxm_date_feed.tzi", False 
     .send 
     s = .responseText 
    End With 
    ' Parse timestamp to Date type 
    d = CDate(DateSerial(Mid(s, 5, 4), Mid(s, 3, 2), Mid(s, 1, 2)) + TimeSerial(Mid(s, 9, 2), Mid(s, 11, 2), Mid(s, 13, 2))) 
    ' Add 4 hours to get UTC from EDT timezone 
    d = DateAdd("h", 4, d) 
    ' Combine URL with timestamp 
    sUrl = "http://www.siriusxm.com/metadata/pdt/en-us/json/channels/siriushits1/timestamp/" & _ 
      LZ(Month(d), 2) & "-" & _ 
      LZ(Day(d), 2) & "-" & _ 
      LZ(Hour(d), 2) & ":" & _ 
      LZ(Minute(d), 2) & ":" & _ 
      "00" 
    ' Retrieve channelMetadataResponse JSON data 
    With CreateObject("MSXML2.XMLHTTP") 
     .Open "GET", sUrl, False 
     .send 
     s = .responseText 
    End With 
    ' Parse JSON response 
    JSON.Parse s, vJSON, sState 
    ' Check if valid 
    If sState <> "Object" Then 
     MsgBox "Invalid JSON response" 
     Exit Sub 
    End If 
    ' Check if available 
    If vJSON("channelMetadataResponse")("messages")("code") <> "100" Then 
     MsgBox "Unavailable content" 
     Exit Sub 
    End If 
    ' Get necessary properties 
    Set vJSON = vJSON("channelMetadataResponse")("metaData")("currentEvent") 
    sArtists = vJSON("artists")("name") 
    sComposer = vJSON("song")("composer") 
    sAlbum = vJSON("song")("album")("name") 
    sSong = vJSON("song")("name") 
    ' Output results 
    MsgBox "On the Air" & vbCrLf & _ 
     "Artists: " & sArtists & vbCrLf & _ 
     "Composer: " & sComposer & vbCrLf & _ 
     "Album: " & sAlbum & vbCrLf & _ 
     "Song: " & sSong 

End Sub 

Function LZ(n As String, q As Long) As String ' Add leading zeroes 
    LZ = Right(String(q, "0") & n, q) 
End Function 

順便說一句,在thisthisthis答案使用相同的方法。

+0

謝謝omegastripes。我添加了json.bas和所有適當的引用。我有一個問題,行JSON.Parse s,vJSON,sState我得到的錯誤說:「編譯錯誤:錯誤數量的參數或無效的屬性分配」當我去json.bas文件中的解析函數,它看起來就像它只查找一個字符串,在這種情況下是變量s。如果我更改爲「JSON.Parse s」,我可以讓它運行,但sState從未分配一個值,並在接下來的幾行中退出。如果我跳過該語句,嘗試設置vJSON時會出現「類型不匹配」錯誤。謝謝。 – mh2017

+0

@ mh2017我貼的JSON.bas的鏈接是錯誤的,我編輯了答案,請檢查鏈接。 – omegastripes

+0

是的,它做到了。謝謝您的意見。實際上你教了我很多。 – mh2017