VBA and Scraping a table from web: how can I detect an image?

Nelson78

Well-known Member
Joined
Sep 11, 2017
Messages
526
Office Version
  1. 2007
Hello everybody.

I use to scrap data from a table with the following code:

VBA Code:
Dim tbls, tbl, trs, tr, tds, td, R, c
Set tbls = IE.document.getElementsByTagName("table")

For R = 0 To tbls.Length - 1
Debug.Print R, tbls(R).Rows.Length
Next R

Set tbl = IE.document.getElementsByTagName("table")(8)
Set trs = tbl.getElementsByTagName("tr")



For R = 0 To trs.Length - 1

Set tds = trs(R).getElementsByTagName("td")

If tds.Length = 0 Then Set tds = trs(R).getElementsByTagName("th")

For c = 0 To tds.Length - 1
Worksheets("Sheets2").Range("A1").Offset(R, c).Value = tds(c).innerText
Next c

Next R

This works perfectly, except for a point.

In one of the columns of the table, sometime it is possible to find an image.

I can't detect it with the code above.

Parsing the framework this way:

trs
item2
cells
item13
innerhtml

I can found inside the value of that innerhtml an item:

VBA Code:
title = Image07

That is the info I need for any row of the table, answering the question: is there the Image07? (Yes/No).


How can I figure it out?
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
The following code assumes that the image will be found in the third column. And so it loops through each row within the third column, and checks whether there's an image by the name "Image07". If so, it prints its source to the Immediate Window, and exits the For/Next loop. Change the cell index for which to search as desired. For example, to search the first column in the row instead, replace...

VBA Code:
Set img = .Item(i).Cells(2).querySelector("img")

with

VBA Code:
Set img = .Item(i).Cells(0).querySelector("img")

Note that the indexing for cells starts at zero. Here's the code...

VBA Code:
    Dim img As Object
    Dim i As Long
  
    On Error Resume Next
    With trs
        For i = 0 To .Length - 1
            Set img = .Item(i).Cells(2).querySelector("img") 'check third column (2 as the index number since cells is zero-based)
            If Err.Number = 0 Then
                If img.getAttribute("title") = "Image07" Then
                    Debug.Print img.getAttribute("src")
                    Exit For
                End If
            Else
                Err.Clear
            End If
        Next i
    End With
    On Error GoTo 0

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,223,961
Messages
6,175,652
Members
452,664
Latest member
alpserbetli

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top