我有一个与HTML解析有关的问题。我的网站上有一些产品,我想将图像中的URL捕获到当前的电子表格中。该电子表格相当大,但在第3列中包含ItemNbr,我希望第27列中的URL一行对应于一个产品(项目)。
我的想法是获取“常规”或“大”或“非常大”图像的URL(这并不重要)。这是网站的结构(除其他div外):
<div id="MainDisplay" class="miMaindisplay">
<a href="http://www.example.com/verylarge/12425/nl" id="ctl00_PageContent_MultiImage_jqzoom" class="loupe">
<div class="zoomPad">
<img src="http://www.example.com/regular/12425/nl" id="ctl00_PageContent_MultiImage_PreviewImage" class="miPreviewImage">
<div class="zoomPup"></div>
<div class="zoomWindow">
<div class="zoomWrapper">
<div class="zoomWrapperTitle"></div>
<div class="zoomWrapperImage">
<img src="http://www.example.com/large/12425/nl">
</div>
</div>
</div>
<div class="zoomPreload">Loading zoom</div>
</div>
</a>
</div>
我可以使用以下行在JS控制台中获取URL:
document.getElementById('ctl00_PageContent_MultiImage_PreviewImage').src;
答案是:
http://www.example.com/regular/12425/nl
但是在VBA中没有成功。这是我的代码段:
Sub ParseImage()
Dim Cell As Integer
Dim ItemNbr As String
Dim AElement As Object
Dim AElements As IHTMLElementCollection
Dim IE As MSXML2.XMLHTTP60
Set IE = New MSXML2.XMLHTTP60
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLBody As MSHTML.HTMLBody
Set HTMLDoc = New MSHTML.HTMLDocument
Set HTMLBody = HTMLDoc.body
For Cell = 1 To 5 'I iterate through the file row by row
ItemNbr = Cells(Cell, 3).Value 'ItemNbr are in the 3rd Column of my spreadsheet
IE.Open "GET", "http://www.example.com/?item=" & ItemNbr, False
IE.send
While IE.ReadyState <> 4
DoEvents
Wend
HTMLBody.innerHTML = IE.responseText
Set AElements = HTMLDoc.getElementsByTagName("a")
For Each AElement In AElements
If AElement.id = "ctl00_PageContent_MultiImage_PreviewImage" Then
Cells(Cell, 27) = AElement.src 'I write URL in the 27th column
End If
Next AElement
Application.Wait (Now + TimeValue("0:00:2"))
Next Cell
结束子
我显然提供了一些参考资料,如下所示:
谢谢您的帮助!
如果要定位的元素由HTML页面中的ID标识,则更直接的方法是使用HTML文档对象的getElementById方法。
尝试替换此部分
Set AElements = HTMLDoc.getElementsByTagName("a")
For Each AElement In AElements
If AElement.id = "ctl00_PageContent_MultiImage_PreviewImage" Then
Cells(Cell, 27) = AElement.src 'I write URL in the 27th column
End If
Next AElement
用类似的东西
set previewImg = HTMLDoc.getElementById("ctl00_PageContent_MultiImage_PreviewImage")
If not previewImg is Nothing then Cells(Cell, 27) = previewImg.getAttribute("src")
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句