在Excel VBA中获取图像url
我需要获取图像的网址,我不需要图像本身,我只需要网址。但我无法让它发挥作用。
我没有编码经验,所以如果有更简单的方式请分享。 这是代码:
Dim i, sonsat As Integer
Dim url As String
Dim XMLreq As New MSXML2.XMLHTTP60
Dim HTMLdoc As New MSHTML.HTMLDocument
sonsat = Sheets("Sayfa1").Range("A10000").End(xlUp).Row
For i = 2 To sonsat
On Error Resume Next
url = Sheets("Sayfa1").Range("A" & i)
XMLreq.Open "GET", url, False
XMLreq.send
If XMLreq.Status <> 200 Then
MsgBox "Sayfaya Ulaşılamadı"
Exit Sub
End If
HTMLdoc.body.innerHTML = XMLreq.responseText
Sheets("Sayfa1").Range("C" & i) = HTMLdoc.getElementsByClassName("title")(0).innerText
Sheets("Sayfa1").Range("B" & i) = HTMLdoc.getElementsByClassName("title sub")(0).innerText
Sheets("Sayfa1").Range("D" & i) = HTMLdoc.getElementsByClassName("proAttr sku")(0).innerText
Sheets("Sayfa1").Range("E" & i) = HTMLdoc.getElementsByclassName("item")(391).innerText
Next
End Sub
I need to get the url of the image, i don't need the image itself i just need url. But i can't get it to work.
Here's the site: https://www.bauhaus.com.tr/hirdavat-hirdavat-urunleri-menteseler-mobilya-menteseleri/adilon-karyola-demiri-61185746
I have no experience in coding, so if there's an easier way please share it.
Here's the code:
Dim i, sonsat As Integer
Dim url As String
Dim XMLreq As New MSXML2.XMLHTTP60
Dim HTMLdoc As New MSHTML.HTMLDocument
sonsat = Sheets("Sayfa1").Range("A10000").End(xlUp).Row
For i = 2 To sonsat
On Error Resume Next
url = Sheets("Sayfa1").Range("A" & i)
XMLreq.Open "GET", url, False
XMLreq.send
If XMLreq.Status <> 200 Then
MsgBox "Sayfaya Ulaşılamadı"
Exit Sub
End If
HTMLdoc.body.innerHTML = XMLreq.responseText
Sheets("Sayfa1").Range("C" & i) = HTMLdoc.getElementsByClassName("title")(0).innerText
Sheets("Sayfa1").Range("B" & i) = HTMLdoc.getElementsByClassName("title sub")(0).innerText
Sheets("Sayfa1").Range("D" & i) = HTMLdoc.getElementsByClassName("proAttr sku")(0).innerText
Sheets("Sayfa1").Range("E" & i) = HTMLdoc.getElementsByclassName("item")(391).innerText
Next
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码大致正确,但事实证明图像是在页面加载后插入的,可能是由 JavaScript 组件插入的。因此,当您获取文档时,它不包含图像灯箱。
但是,该图像在页面元数据中被引用为
因此您可以从那里提取它。
还有一些关于干净编码的建议:
New MSXML2.XMLHTTP60
和New MSHTML. HTMLDocument
在循环内)HTMLDocument
使用后应该关闭所以这是一段非常接近您的最终要求的代码(我只是改变了循环,所以我不必重新制作你的 Excel 工作簿在我的侧面:
最后一点:要查看 Debug.Print 的输出,请使用 VBA 中的执行窗口
Your code is about right, but it turns out the image is inserted after page load, probably by a JavaScript component. So when you get the document, it does not contain the image lightbox.
However, the image is referenced in the page metadata as
So you can pull it from there.
Also some advice about clean coding:
New MSXML2.XMLHTTP60
andNew MSHTML.HTMLDocument
inside the loop)HTMLDocument
should be closed after useSo here's a piece of code very close to your final requirements (I just changed the loop so I didn't have to re-make your Excel Workbook on my side:
And final note: to see the ouput of
Debug.Print
, use the Execution Window in VBA