How do I use VBA to scrape song Title&Artist from streaming website

inventiveman

New Member
Joined
Jul 30, 2013
Messages
8
Hi, I'm new to Web page scraping using VBA in Excel and hoping somesome can help me.

I'd like to listen to web streamed music while I work on my computer and would like the current playing song's Artist & Title displayed in a small window near the top or bottom of the screen. That window could be Notepad, or even in the StatusBar of Excel. I can do all the coding except scrape the data.

I've followed a good web tutorial on Wise Owl
but suspect these techniques may not work on webpages that contain javascript.

I have managed to do this using VBA & IE but it's unreliable. Sometimes IE doesn't fully load & freezes, and when it does work often the Artist & Title isn't displayed for 70 or more seconds and the song is half over.

So I've tried using XML to bypass the use of IE but that raises ERROR 91 ObjectVariableNotSet or other errors that I don't have the experience to fix.

The VBA code, and comments relating to problems, I've used for one of the Radio Stations is:
VBA Code:
Sub RadioStationSongIE()
' appropriate Libraries are Referenced

    Dim IE As New SHDocVw.InternetExplorer
    Dim HTMLDoc As MSHTML.HTMLDocument
    Dim HTMLTagNames As MSHTML.IHTMLElementCollection
    Dim HTMLTagName As MSHTML.IHTMLElement
    Dim Song As String
    
    IE.Visible = True
    IE.navigate "https://www.kiis1065.com.au/"
    Do While IE.ReadyState <> READYSTATE_COMPLETE
    ' IE sometimes doesn't fully load
    DoEvents 'Escape loop with CTRL-Break when page doesn't fully load
    Loop
    Set HTMLDoc = IE.Document
    
    ' THIS WORKS, BUT often takes 70 seconds + to get & display new Song
    ' if no Song (or special segment) is being Played then a generic message is displayed
    Set HTMLTagNames = HTMLDoc.getElementsByClassName("po-audio-player__component-on-air__name")
    Song = HTMLTagNames(0).Title
    Application.StatusBar = Song
    Debug.Print Song
    
    For Each HTMLTagName In HTMLTagNames   'Check in case there's more than 1 TagName
        Debug.Print HTMLTagName.Title
    Next HTMLTagName
        
    IE.Quit
End Sub

Sub RadioStationSongXML()
' this is the same as RadioStationSongIE except using XML & "GET"
' BUT raises ERROR 91 ObjectVariableNotSet

    Dim XMLPage As New MSXML2.XMLHTTP60
    Dim HTMLDoc As New MSHTML.HTMLDocument
    Dim HTMLTagNames As MSHTML.IHTMLElementCollection
    Dim HTML_ID As MSHTML.IHTMLElement
    
    XMLPage.Open "GET", "https://www.kiis1065.com.au/", False
    XMLPage.send
    HTMLDoc.body.innerHTML = XMLPage.responseText
    
    Set HTMLTagNames = HTMLDoc.getElementsByClassName("po-audio-player__component-on-air__name")
    Song = HTMLTagNames(0).Title    'This raises ERROR 91 ObjectVariableNotSet
    Application.StatusBar = Song
    Debug.Print Song
    
    For Each HTMLTagName In HTMLTagNames   'Check in case there's more than 1 TagName
        Debug.Print HTMLTagName.Title
    Next HTMLTagName

' 
' COMMENTS
' The other 2 Preferred web pages I've tried (Fast in displaying current Song) are:
'
' https://lava.net.au/stations
' KIIS 1065 (Row3 Column2)
' 
' or 'Player' on this page
' https://player.lava.net.au/kiis1065
' A minute or so after the 'Play' (►) button is pressed a 'Playing Now' section opens and shows the Title & Artist for the current song.
' This section then remains open irrespective of whether the Play or Pause button is showing.
' A generic message shows when there's no song playing.
' But when using Firefox, the 'Inspect(ed) Elements' that show the songs, don't seem to be available to VBA,
' or if they are I don't know how to get them.
'
' The Title & Artist is displayed shortly after the Song starts so either of these is my preferred webpage to scrape.
'
    Debug.Print Song

End Sub

Many home radio stations are often slow to display the current playing song but the site I've found is fast in doing so I'd prefer to use it.

Can anyone please help?
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

diddi

Well-known Member
Joined
May 20, 2004
Messages
3,337
Office Version
  1. 2010
Platform
  1. Windows
Stick this in your web browser. it might give you some ideas
VBA Code:
https://au.api.iheart.com/api/v3/live-meta/stream/6185/currentTrackMeta
 

inventiveman

New Member
Joined
Jul 30, 2013
Messages
8

ADVERTISEMENT

Thanks for the link diddi.
It does work well for any of the iHeart radio stations but unfortunately iHeart only provides a few of the stations that I listen to and those streams (& Meta, which is what I'm really interested in) are only available (AFAIK) from their webpages.

The main problem that I have is that I have a hearing loss & use hearing aids. These can get uncomfortable if used all day. So I have an old style FM-tuner connected to an equaliser & this corrects for my hearing loss & I don't have to wear the hearing aids all day. Running a streamed station through an equaliser isn't possible on my PC. And at the moment the last Win10 update has totally killed my audio!

Now, streamed radio such as iHeart is delayed by somewhere between 30 & 90 seconds, sometimes even 2 minutes compared to 'over the air' radio transmissions. This means any Title/Artist info from a stream is out of sync with the 'over the air' signal. Listening to a song and seeing the last played song displayed for so long can be quite annoying.

I take your point on indenting & usually do this, as well as leaving a blank line & comment(s) between sections, but on this occasion I missed the first loop.
 

diddi

Well-known Member
Joined
May 20, 2004
Messages
3,337
Office Version
  1. 2010
Platform
  1. Windows
every website is different. i just check the one you linked. if you post more links i can see if there is a simple way to access the info with your ie automation method which you are familiar with. or you can check them your self...
1613516227370.png
 

inventiveman

New Member
Joined
Jul 30, 2013
Messages
8

ADVERTISEMENT

diddi, I really appreciate your suggestions, and when I setup my new PC I'll use your last one to reprogramme my VBA macro for various radio stations and see if they're any better than the Lava website option that I've been using. Lava is a really good website for various radio stations.
Cheers
 

diddi

Well-known Member
Joined
May 20, 2004
Messages
3,337
Office Version
  1. 2010
Platform
  1. Windows
np. get back to me if you want to explore any more ideas. :) good luck with new PC setup
 

Forum statistics

Threads
1,147,482
Messages
5,741,404
Members
423,657
Latest member
Medrok2021

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top