我需要从http://www.siriusxm.com/siriusxmhits1拉出当前播放的艺术家和歌曲。我可以通过Internet Explorer导航到网站,但需要太长时间,所以我尝试使用WINHTTP.WinHTTPRequest.5.1
和MSXML2.serverXMLHTTP
,但都没有提取我正在寻找的具体数据。我想我很接近,但我错过了什么。
以下是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 :(得分:0)
网站http://www.siriusxm.com有一种可用的API。我通过Chrome中的链接http://www.siriusxm.com/hits1浏览了一个页面,然后打开了“开发人员工具”窗口( F12 ),“网络”标签,并检查了列表中的XHR。可以检索当前歌曲信息。 G。在以下步骤中:
按网址http://www.siriusxm.com/sxm_date_feed.tzi制作XHR以检索当前时间戳。
使用最后一个网址http://www.siriusxm.com/metadata/pdt/en-us/json/channels/siriushits1/timestamp/04-29-02:02:55中的当前时间戳创建XHR
解析收到的JSON响应。
将歌曲名称设为JSON.channelMetadataResponse.metaData.currentEvent.song.name
,将艺术家设为JSON.channelMetadataResponse.metaData.currentEvent.artists.name
等。
以下是显示JSON响应结构的示例,我使用在线工具http://jsonviewer.stack.hu:
您可以使用以下VBA代码检索上述信息。 将JSON.bas模块导入VBA项目以进行JSON处理。
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