Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Extract the link of image from website and copy it into excel

0

Hi,

I saw this tutorial of TeachExcel, it was helpful to extract the text in website into excel.

But my problem here, is that i need to extract an image that is into a "div" like that : 

<div class="slide-image" style="background-image: url('/content/dam/hikvision/products/HIKVISION/Turbo_HD_Products/Turbo_HD_Cameras/Value_Series/D0T_Series/DS-2CE56D0T-VPIR3F/images/2CE56D0T-半球11-正视图.png.thumb.1280.1280.png');"></div>

So, i need to go this website : https://www.hikvision.com/en/products/Turbo-HD-Products/Turbo-HD-Cameras/Value-Series/ds-2ce56d0t-vpir3f/

Search the class "slide-image" and extract the link of the image to copy it into my excel...

Sub Get_Web_Data()

' TeachExcel.com

Dim request As Object

Dim response As String

Dim html As New HTMLDocument

Dim website As String

Dim price As Variant

' Website to go to.

website = "https://www.hikvision.com/en/products/Turbo-HD-Products/Turbo-HD-Cameras/Value-Series/ds-2ce56d0t-vpir3f/"

' Create the object that will make the webpage request.

Set request = CreateObject("MSXML2.XMLHTTP")

' Where to go and how to go there - probably don't need to change this.

request.Open "GET", website, False

' Get fresh data.

request.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"

' Send the request for the webpage.

request.send

' Get the webpage response data into a variable.

response = StrConv(request.responseBody, vbUnicode)

' Put the webpage into an html object to make data references easier.

html.body.innerHTML = response

' Get the price from the specified element on the page.

Image = html.getElementsByClassName("slide-image").Item(0).innerText

' Output the price into a message box.

MsgBox Name

End Sub

Please help me !!!! 

Answer
Discuss

Answers

0

I checked your code and made some minor changes pertaining to declartions - nothing significant. Here is the version I tested.

Option Explicit

Sub Get_Web_Data()
    ' TeachExcel.com

    Dim website As String
    Dim request As Object
    Dim response As String
    Dim html As New HTMLDocument
    Dim Element As Object
    Dim image As String

    ' Website to go to.
    website = "https://www.hikvision.com/en/products/Turbo-HD-Products/Turbo-HD-Cameras/Value-Series/ds-2ce56d0t-vpir3f/"

    ' Create the object that will make the webpage request.
    Set request = CreateObject("MSXML2.XMLHTTP")

    ' Where to go and how to go there - probably don't need to change this.
    request.Open "GET", website, False
    ' Get fresh data.
    request.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"

    ' Send the request for the webpage.
    request.send
    ' Get the webpage response data into a variable.
    response = StrConv(request.responseBody, vbUnicode)

    ' Put the webpage into an html object to make data references easier.
    html.body.innerHTML = response

    ' Get the image name from the specified element on the page.
    image = html.getElementsByClassName("slide-image").Item(0).innerText

    ' Output the image name into a message box.
    MsgBox image
End Sub

This code works. It retruns a null string. The element identified by class name "slide-image" does exist and its Type(0) is found. I presume that the site owners have taken precautions to prevent scraping of their IP. The name you are looking for might be kept on another server and merely referenced by the field you are looking at. Something like an Excel cell that displays the contents of a data source but when you try to read what's in the cell you get a formula. Here you get nothing at all. An instruction where to get the contents of that Item is elsewhere on the site.

It could be a completely different reason, such the info not being where you say it is. It can probably still be scraped either way but that requires research for your paticular project that goes way beyond what this forum intends to do.

Discuss


Answer the Question

You must create an account to use the forum. Create an Account or Login