如何从VBA中的img标签parsingsrc

我有一个关于HTMLparsing的问题。 我有一个网站的一些产品,我想从图像捕捉到我当前的电子表格中的url。 这个电子表格是相当大的,但在第三列包含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方法。

尝试并replace此部分

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