Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel VBA web scraping from table

I am trying to extract some info from the table below into Excel using VBL without any success. The values which I need do not seem to have any element ID, tag name or class name assigned to it. I'm after the Fuel Usage value(89218) and the time value in the same row (01:15). Can anyone point me in the right direction on how to scrape values from a table, or how to extract data from specific TR, TD.

HTML source of the table:

<h3>Airbus A300-600-PW4158 Fuel Planner</h3>
<p>London to Chicago  EGKK-KORD (3441 NM)<br /></p>
<h2>Total Fuel: 101901 POUNDS</h2>
<table width="100%" border=1>
    <tr>
        <th style="text-align:left;">&nbsp;</th>
        <th style="text-align:left;">Fuel</td>
        <th style="text-align:left;">Time</th>
    </tr>
    <tr>
        <td>Fuel Usage</td>
        <td>89218</td>
        <td>08:47</td>
    </tr>
    <tr>
        <td>Reserve Fuel</td>
        <td>12682</td>
        <td>01:15</td>
    </tr>
    <tr>
        <td>Fuel on Board</td>
        <td>101901</td>
        <td>10:02</td>
    </tr>
</table>

much appreciated.

like image 616
Slavik S Avatar asked Feb 04 '26 11:02

Slavik S


1 Answers

CSS Selectors:

Without seeing more of the HTML you can use the following CSS selectors selectors for the snippet shown:

tr td:nth-child(2)
tr td:nth-child(3)

With CSS selectors this will bring back nodeLists of all 2 or 3 child tds with a tr.

For example:

CSS Selectors

You can access individual items from a nodeList by index.


VBA:

The syntax in vba overall will be something like:

.document.querySelectorAll("tr td:nth-child(2)")(0).innerText

or possibly

.document.querySelectorAll("tr td:nth-child(2)").Item(0).innerText

The 0 is hypothetical. You would need to inspect your full HTML to ascertain the correct index to use.

The .document innerHTML can be populated from the .responseText using IE, for example, to navigate to the page.

like image 97
QHarr Avatar answered Feb 06 '26 02:02

QHarr