Extract Text from Web via VBA

rdormond

New Member
Joined
Jun 1, 2015
Messages
11
Hello All,

I am having trouble being able to capture what I need from an HTML website to extract data for my VBA code.

My code automatically uses a webpage to search the distance between two airports. That part works fine, extracting the actual value of the distance is giving me problems.

The website I am extracting data from is
HTML:
http://www.flightmanager.com/content/timedistanceform.aspx
and my code inputs LAX and ATL as the airports.
The data I want to retrieve is: "1,946.88"
And that could be found where it says "Distance," it should look like this...
Distance: 1,691.43 (NM) / 1,946.88 (MI) / 3,132.53 (KM)

Code:
Sub parse_distances()


Dim air1, air2 As String
air1 = "ATL"
air2 = "LAX"
' variables are here to check if this code works, DELETE later


Set objIEBrowser = CreateObject("InternetExplorer.Application")
    '   open Internet Explorer


   objIEBrowser.Visible = True
   '    allows you to see the webpage


   objIEBrowser.Navigate2 "http://www.flightmanager.com/content/timedistanceform.aspx"
   '    opens up the website I will use to find the airport distances
   
   Do While objIEBrowser.Busy
   Loop
   Do While objIEBrowser.readyState < 4
   Loop
   '    prevents anything else from happening while the site is loading.
   
   Set objPage = objIEBrowser.document
   '    idk what this is, i'm just trying to follow similar code from an online source
   
   Set Airport1 = objPage.getElementById("ctl00_ContentPlaceHolder1_txtDepartureICAO")
   Airport1.Value = air1
   Set Airport2 = objPage.getElementById("ctl00_ContentPlaceHolder1_txtArrivalICAO")
   Airport2.Value = air2
   '    Inputs the airport names into the search engine.
   
   Set clickCalcDistButton = objPage.getElementById("ctl00_ContentPlaceHolder1_BtnSubmit")
   clickCalcDistButton.Click
   
   Do While objIEBrowser.Busy
   Loop
   Do While objIEBrowser.readyState < 4
   Loop
   '    prevents anything else from happening while the site is loading.
   
   Dim dist As String
   dist = IE.Dcoument.getElementsByClassName("std5")(1).innerText
   ' THIS IS WHERE I'M HAVING TROUBLE.


End Sub

Tips and help are always appreciated!
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
This line :-
Code:
 dist = IE.Dcoument.getElementsByClassName("std5")(1).innerText
is incorrect it should be :-
Code:
 dist = IE.Dcoument.getElementsByClassName("td5")(10).innerText

you may have to change the element from 10, either up or down.

hth
 
Upvote 0
Thank you for the response, I also noticed a spelling error in that line of code you mentioned and fixed it. So I've tried the following from element 0 to 15 to no avail.

Code:
[COLOR=#333333] dist = IE.document.getElementsByClassName("td5")(10).innerText[/COLOR]

Any other suggestions you might have?
 
Upvote 0
Hi

That line needs to be :-
Code:
  dist = objPage.getElementsByClassName("td5")(10).innerText

Apologies that I didn't spot either the spelling mistake or that fact that it was referring to the wrong object.

May I suggest changing all instances of the following :-
Code:
   Do While objIEBrowser.Busy
   Loop
   Do While objIEBrowser.readyState < 4
   Loop
to :-
Code:
   Do While objiebrowser.Busy: DoEvents:  Loop
   Do While objiebrowser.ReadyState < 4: DoEvents:  Loop
to enable the successful completion of those statements.

Also objPage is defined so that you don't have to refer to objIEBrowser.Document both when you are setting the fields on and retrieving data from the webpage.

hth
 
Upvote 0
And to get the distance out as a standalone figure as per OP :-
Code:
    dist = Trim(Left(Split(objPage.getElementsByClassName("td5")(10).innerText, "/")(1), 10))

hth
 
Upvote 0
Mike,

Your advice and corrections worked wonderfully! Thank you for the help, I truly appreciate it!

-Ryan
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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
Back
Top