首页 > 解决方案 > 使用excel vba提取图像url

问题描述

我正在使用 excel 2016。我需要在 excel 中使用 VBA 从网站中提取图像的链接。

例如,我有一个网站显示带有链接的产品:https ://www.hikvision.com/en/products/Turbo-HD-Products/Turbo-HD-Cameras/Value-Series/ds-2ce56d0t-vpir3f/

我的图像进入 div ,如下所示:

<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>

我试过这个:

Private Sub btnExtractURL_Click()
Dim sourceString As String
Dim rowIdx As Integer, rowMax As Integer
Dim posFirst As Integer, posLast As Integer, chrLength As Integer
rowMax = Range("A3").End(xlDown).Row
' ---
For rowIdx = 3 To rowMax
    If Cells(rowIdx, 1).Value <> "" Then
        Cells(rowIdx, 2).Value = ""
        sourceString = Cells(rowIdx, 1).Value
        posFirst = InStr(sourceString, "http")
        posLast = InStr(posFirst, sourceString, """")
        chrLength = (posLast - 1) - (posFirst - 1)
        Cells(rowIdx, 2).Value = Mid(sourceString, posFirst, chrLength)
    End If
Next
' ---
MsgBox "finished"
End Sub

但是我对这个解决方案有一个错误......我试图提取文本以查看另一种方法,它可以工作,但是当我插入该图像的类时,它不起作用!

Sub Get_Web_Data()

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.
Name = html.getElementsByClassName("prod_name").Item(0).innerText

' Output the price into a message box.
MsgBox Name

End Sub

你能提出一个想法来提取这张图片并将链接复制到我的excel中吗?

标签: excelvbaimagehyperlink

解决方案


推荐阅读