VBA Error - Site change get images links

tactos

New Member
Joined
Jan 6, 2019
Messages
7
I already had one person's help to get the code and I got it working without problems.
Now I think the site has changed and now I can not get results.
Code:
[COLOR=#454545][FONT=&quot][B]Public Function getHeroImage(productUrl As String) As String[/B][/FONT][/COLOR]
[COLOR=#454545][FONT=&quot]
[/FONT][/COLOR]
[COLOR=#454545][FONT=&quot][B]    Dim dom As HTMLDocument[/B][/FONT][/COLOR]
[COLOR=#454545][FONT=&quot][B]    Set dom = New HTMLDocument[/B][/FONT][/COLOR]
[COLOR=#454545][FONT=&quot][B]    With CreateObject("winhttp.winhttprequest.5.1")[/B][/FONT][/COLOR]
[COLOR=#454545][FONT=&quot][B]        .Open "GET", productUrl, False[/B][/FONT][/COLOR]
[COLOR=#454545][FONT=&quot][B]        .send[/B][/FONT][/COLOR]
[COLOR=#454545][FONT=&quot][B]        dom.body.innerHTML = .responseText[/B][/FONT][/COLOR]
[COLOR=#454545][FONT=&quot][B]        getHeroImage = dom.querySelector("div.goodsIntro_largeImgWrap > img").getAttribute("data-zoom")[/B][/FONT][/COLOR]
[COLOR=#454545][FONT=&quot][B]    End With[/B][/FONT][/COLOR]
[COLOR=#454545][FONT=&quot]
[/FONT][/COLOR]
[COLOR=#454545][FONT=&quot][B]End Function[/B][/FONT][/COLOR]
I basically wanted to have the links of the images in a column. The code I was using was as follows:
but now it does not return results.
I've tried changing the selector to:
Code:
[COLOR=#454545][FONT=&quot][B]dom.querySelector ("div.goodsIntro_largeImgWrap> # js-goodsNormalImg> img"). getAttribute ("data-zoom")[/B][/FONT][/COLOR]
but it did not work.
Can someone help please? What have I done wrong to change in the next?
Website I want to get the pictures: https://www.gearbest.com/cell-phones...601631974.html
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Re: VBA Eror - Site change get images links

Welcome to the Board

Can you follow the procedure below? It assumes you are using Chrome, but IE is similar.


  • Load the page with your browser.

  • Right click one image you want.
  • Choose the inspect option.
  • At the right pane, the element HTML code will be highlighted.
  • Paste it here, either by typing or as an image.
 
Last edited:
Upvote 0
Re: VBA Eror - Site change get images links

Code:
<img src="https://gloimg.gbtcdn.com/soa/gb/pdm-product-pic/Electronic/2018/11/10/goods_img_big-v1/20181110151249_73645.jpg" data-img="https://gloimg.gbtcdn.com/soa/gb/pdm-product-pic/Electronic/2018/11/10/goods_img_big-v1/20181110151249_73645.jpg" data-gbimg="https://css.gbtcdn.com/imagecache/gbw/img/site/mainDefault.jpg" data-zoom="https://gloimg.gbtcdn.com/soa/gb/pdm-product-pic/Electronic/2018/11/10/source-img/20181110151249_73645.jpg" alt="Xiaomi Mi Note 3 4G Smartphone Versão Internacional" class="goodsIntro_largeImg initial lazy-loaded" id="js-goodsNormalImg" width="500" height="500" data-was-processed="true">
 
Upvote 0
Re: VBA Eror - Site change get images links

Please test this:

Code:
Public Sub GetInfo()
Dim res$, html As HTMLDocument, Elements As Object, i%, j%
With CreateObject("MSXML2.XMLHTTP")
    .Open "GET", "https://www.gearbest.com/cell-phones/pp_009601631974.html", False
    .setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
    .Send
    res = StrConv(.responseBody, vbUnicode)
End With
Set html = New HTMLDocument
[a:a].ClearContents
With html
    .Body.innerHTML = res
    Set Elements = .getElementsByTagName("img")
    j = 0
    For i = 0 To Elements.Length - 1
        If Not IsNull(Elements(i).getAttribute("src")) Then
            j = j + 1
            Cells(j, 1) = Elements(i).getAttribute("src")
        End If
    Next
    MsgBox j, , "#elements found"
End With
End Sub
 
Upvote 0
Re: VBA Eror - Site change get images links

How i run this?
In the other code i did like that image.
g5artPX


And here give me error like this:
GPTeZIG
 
Upvote 0
Re: VBA Eror - Site change get images links

Please test this:

Code:
Public Sub GetInfo()
Dim res$, html As HTMLDocument, Elements As Object, i%, j%
With CreateObject("MSXML2.XMLHTTP")
    .Open "GET", "https://www.gearbest.com/cell-phones/pp_009601631974.html", False
    .setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
    .Send
    res = StrConv(.responseBody, vbUnicode)
End With
Set html = New HTMLDocument
[a:a].ClearContents
With html
    .Body.innerHTML = res
    Set Elements = .getElementsByTagName("img")
    j = 0
    For i = 0 To Elements.Length - 1
        If Not IsNull(Elements(i).getAttribute("src")) Then
            j = j + 1
            Cells(j, 1) = Elements(i).getAttribute("src")
        End If
    Next
    MsgBox j, , "#elements found"
End With
End Sub

I already run the code in VBA (green arrow) and give me error.

Print1: https://ibb.co/FHK6kHg
Print2: https://ibb.co/cYYQbL4

Tks for the help
 
Upvote 0
Re: VBA Eror - Site change get images links

To run the code, press ALT + F8 while on the spreadsheet and pick the desired subroutine.
It will write all found elements at column A.
Here at work I cannot see the images, but remember noticing yesterday at home that you were using the code as a worksheet function, would you like that? Are you expecting multiple results from the web query?
 
Upvote 0
I have always used as a function I do not know another reality.
Normally I put about 100 links (all different from this website) in column A. In column B I had the function that returned the links of the images.
Even so you are giving an error:
Compile Error:
User-defined type not defined

Print error: https://ibb.co/Tb2Xpkq
 
Upvote 0
o Add a reference to the Microsoft HTML object library at VBE > tools > references

The function below will return the first element. If you need multiple results, we will need a subroutine or an array formula.

Code:
Public Function GetInfo$(w$)
Dim res$, html As HTMLDocument, Elements As Object
With CreateObject("MSXML2.XMLHTTP")
    .Open "GET", w, False
    .setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
    .Send
    res = StrConv(.responseBody, vbUnicode)
End With
Set html = New HTMLDocument
GetInfo = "Null"
With html
    .Body.innerHTML = res
    Set Elements = .getElementsByTagName("img")             ' your tag here
    ' your attribute here
    If Not IsNull(Elements(0).getattribute("src")) Then _
    GetInfo = Elements(0).getattribute("src")
End With
End Function
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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