VBA code to scrape data from a website


 Sub ScrapeDataFromWebsite()

    Dim HttpReq As Object

    Dim HTMLDoc As Object

    Dim URL As String

    Dim Element As Object

    Dim Data As String

    

    URL = "https://www.example.com"

    Set HttpReq = CreateObject("MSXML2.XMLHTTP")

    HttpReq.Open "GET", URL, False

    HttpReq.send

    

    Set HTMLDoc = CreateObject("htmlfile")

    HTMLDoc.body.innerHTML = HttpReq.responseText

    

    Set Element = HTMLDoc.getElementById("elementID")

    Data = Element.innerText

    

    Debug.Print Data 'Replace with your desired output action

End Sub

This code uses the MSXML2.XMLHTTP object to make an HTTP request to a website and the htmlfile object to parse the response as HTML. You can customize the URL and element ID to scrape data from any website.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.