Search code examples
htmlexcelvbaweb-scrapinggetelementsbytagname

Scraping specific data inside a table


This may be a pretty easy solution but I'm at a loss even though I feel close.

I'm trying to scrape market data from the TMX website, specifically I would like the dividend amount to drop into my excel workbook.

This is the below few lines of code from the "inspect" section.

<table class="table">
  <tbody>
    <tr>
      <td>
        <strong>Amount:</strong>&nbsp;
        $0.0495
      </td>

I need to get $0.0495 into the workbook and my vba code is the following:

For Each htmlELE In ieObj.document.getElementsByClassName("table")(0).getElementsByTagName("tr")(0).getElementsByTagName("td")
    With ActiveSheet
        .Range("L10").Value = htmlELE.Children(0).innerText
    End With

So, I'm moving through the code but the end keeps picking up "Amount:" and dropping it in the cell instead of the value, what I need, sitting on the next line.


Solution

  • Dim v
    For Each htmlELE In ieObj.document.getElementsByClassName("table")(0). _
                   .getElementsByTagName("tr")(0).getElementsByTagName("td")
    
        v = htmlELE.innerText
    
        '&nbsp; is CHR(160)
        ActiveSheet.Range("L10").Value = trim(Replace(v, "Amount:" & chr(160),""))
    
        'or lookfor the $ and take the rest
        p = Instr(v, "$")
        If p > 0 Then ActiveSheet.Range("L10").Value= Mid(v, p)