' Thread: http://www.mrexcel.com/forum/excel-questions/863282-auto-update-data-same-table-url.html
' Poster: flagen (James Durst) www.mrexcel.com
Global HTMLdoc As Object
Global PageSrc As String
Function GetElemText(ByRef Elem As Object, ByRef ElemText As String, Optional ByVal Suffix As String) As String
' Written: March 18, 2015
' Updated: June 21, 2015
' Author: Leith Ross
' This is a recursive procedure to extract text from between
' an element's start tag and end tag and everything in between.
If Elem Is Nothing Then Exit Function
ElemText = ElemText & Suffix
' Is this element a text value?
If Elem.NodeType = 3 Then
' Separate text elements with a Pipe character.
ElemText = ElemText & Elem.NodeValue & "|"
Else
' Keep parsing - Element contains other non text elements.
For Each Elem In Elem.ChildNodes
' Add new line characters to certain tags.
Select Case UCase(Elem.NodeName)
Case Is = "P": Suffix = "~"
Case Is = "BR": Suffix = "~"
Case Is = "TR": Suffix = "~"
Case Else: Suffix = ""
End Select
Call GetElemText(Elem, ElemText, Suffix)
Next Elem
End If
Finished:
GetElemText = ElemText
Suffix = ""
End Function
Sub ScrapeTables()
Dim IEapp As Object
Dim oDiv As Object
Dim Path As String
Dim n As Variant
Dim oShell As Object
Dim r As Long
Dim ret As Long
Dim running As Boolean
Dim Text As String
Dim URL As Variant
Dim URLs As Variant
Dim Wks As Worksheet
' Select the output worksheet.
Set Wks = ThisWorkbook.Worksheets("Sheet1")
' Clear the output worksheet.
Wks.UsedRange.Clear
' Web site URL list for web pages that have tables to be scraped.
ReDim URLs(1 To 5)
' Assign the URLs to each array element.
URLs(1) = "view-source:https://truckstop.com/Decision_Tools/MainReports.aspx?DecisionTool=LoadDensities#NORTHEAST"
URLs(2) = "view-source:https://truckstop.com/Decision_Tools/MainReports.aspx?DecisionTool=LoadDensities#SOUTHERN"
URLs(3) = "view-source:https://truckstop.com/Decision_Tools/MainReports.aspx?DecisionTool=LoadDensities#MIDWEST"
URLs(4) = "view-source:https://truckstop.com/Decision_Tools/MainReports.aspx?DecisionTool=LoadDensities#PLAINS"
URLs(5) = "view-source:https://truckstop.com/Decision_Tools/MainReports.aspx?DecisionTool=LoadDensities#WESTERN"
Set oShell = CreateObject("Shell.Application")
' Get the running Internet Explorer application object.
For n = 0 To oShell.Windows.Count - 1
If Not oShell.Windows(n) Is Nothing Then
If oShell.Windows(n).Name = "Internet Explorer" Then
running = True
Set IEapp = oShell.Windows(n)
Exit For
End If
End If
Next n
' Is there a running instance of Internet Explorer?
If Not running Then
MsgBox "Internet Explorer is Not Running"
Exit Sub
End If
' Scrape table data from each URL in the list.
For Each URL In URLs
Application.Cursor = xlWait
IEapp.Navigate "about:blank"
While IEapp.Busy: DoEvents: Wend
IEapp.Navigate URL
Set HTMLdoc = IEapp.Document
' Wait until the table becomes available.
On Error Resume Next
Do
DoEvents
Set oDiv = HTMLdoc.getElementById("ctl00_ContentPlaceHolder_divDensity")
If Not oDiv Is Nothing Then Exit Do
Loop
On Error GoTo 0
Application.Cursor = xlDefault
' Get the table data.
Text = ""
Text = GetElemText(oDiv, Text)
' Remove HTML new line characters.
Text = Replace(Text, vbLf, "")
' Change HTML Non Breaking Space character to a space.
Text = Replace(Text, Chr(160), " ")
Lines = Split(Text, "~")
For n = 4 To UBound(Lines)
Text = Lines(n)
data = Split(Text, "|")
' Format the table output.
With Wks.Range("A2")
Select Case n
Case 4: .Offset(r, 0).Value = data(2)
Case 5: .Offset(r, 3).Value = data(3): .Offset(r, 11).Value = data(5)
Case Is > 5: .Offset(r, 0).Resize(1, UBound(data)).Value = data
End Select
End With
r = r + 1
Next n
Set HTMLdoc = Nothing
Next URL
End Sub