VBScript Extract Data – Xmlhttp Web Scrape – Parse HTML

Extract Data From Website Using VBS (vbscript)

To get website data to using vbscript (.vbs) file, we need to know 2 processes. First one is to extract HTML code from website & then parse required data from HTML elements or tags.

Objects Used: 

  • CreateObject(“MSXML2.XMLHTTP”) ‘ To sent xmlhttp request to webserver & get html code.
  • CreateObject(“htmlfile”) ‘ To parse data from html tags

To parse data from html, we can use the below commands on a htmlfile object.

  • getElementById(“idname”)
  • getElementsByClassName(“classname”)
  • getElementsByTagName(“tagname – a,p,div,table,td,tr etc.,”)
  • DOMobject.getAttribute(“attributename – title, href,class,id etc.,”)
  • DOMobject.parentElement.NextSibling

There are plenty of functions like this to traverse through each tags or elements in a valid html file. Now, lets see a working example. To test this code, replace the URL with a valid website address.

myURL = "http://URL.com"

'Create XMLHTTP Object & HTML File
Set oXMLHttp = CreateObject("MSXML2.XMLHTTP")
Set ohtmlFile = CreateObject("htmlfile")

'Send Request To Web Server
oXMLHttp.Open "GET", myURL, False
oXMLHttp.send

'If Return Status is Success
If oXMLHttp.Status = 200 Then

    'Get Web Data to HTML file Object
    ohtmlFile.Write oXMLHttp.responseText
    ohtmlFile.Close
        
    'Parse HTML File
    Set oTable = ohtmlFile.getElementsByTagName("table")
    For Each oTab In oTable
        MsgBox oTab.Innertext
    Next
End If

'Process Completed
WScript.Quit

To execute this code, copy paste this code to a text editor & save the file as “testing.vbs”. Then double click on this file, Windows OS will start to execute the commands one by one.

Reference:

Leave a Reply