Esdras Ribeiro
Esdras Ribeiro

Reputation: 11

Scrape html data Vba

I want to make a function that extracts data from a part of a site.

The following is the HTML site. HTML code.

Code for the function

Function GetElementById(url As String, id As String, Optional isVolatile As Boolean)
    Application.Volatile (isVolatile)
    On Error Resume Next
    Dim html As Object, objResult As Object
    ret = GetPageContent(url)
    Set html = CreateObject("htmlfile")
    html.Body.innerHtml = ret
    Set objResult = html.GetElementById(id)
    GetElementById = objResult.innerHtml
End Function

I need that extracts only the class "panel-body" directly into the function. I think it would be .children (3). Is that correct?

And so that it is practical and fast, because I need to extract more than 50 sites.

Upvotes: 1

Views: 622

Answers (2)

Kharoof
Kharoof

Reputation: 597

Another way to generate the code for this job is to record a macro, then add a loop around the recorded macro that loops through your 50 pages and gets the results.

On the data tab in the ribbon there is an option get data from external sources. If you use this it's gives you a point and click interface that let's you chose the table your looking for. Record a macro while your doing this and it generates the code for you.

Upvotes: 0

darbid
darbid

Reputation: 2731

I see at least two options.

  1. Once you have the HTMLDivElement with id=Result you could simply get the children. Please test this by first doing objResult.Children(2) and checking what the element is that is returned.

    objResult.Children(2).Children(0).Children(0)

  2. The second is that in later versions of MSHTML I think with IE8 or later installed you have the method "GetElementsByClassName" This will return a collection of IHTMLElements. If the HTMLDocument only has 1 "panel-body" then you are in luck. If not you would need to loop through each one and check some other unique feature to know you have the right one.

Upvotes: 0

Related Questions