在VBA中按类名获取图像src

在VBA中按类名获取图像src,vba,excel,Vba,Excel,我试图从一个页面获取大图像的url <ul id="etalage"> <li class=" product-image-thumbs" > <img class="etalage_source_image_large" src="http://website.com/media/1200x1200/16235_1.jpg" title="" /> <img class="etalage_source_image_small" src="http://

我试图从一个页面获取大图像的url

<ul id="etalage">
<li class=" product-image-thumbs" >
<img class="etalage_source_image_large" src="http://website.com/media/1200x1200/16235_1.jpg" title="" />
<img class="etalage_source_image_small" src="http://website.com/media/450x450/16235_1.jpg" title="" />
</li>
</ul>
我的vba代码是

Public Sub macro1()
Dim ie As Object
Dim name As String
  Do Until IsEmpty(ActiveCell)
    ActiveCell.Offset(0, 1).Value = "RUNNING"
    URL = Selection.Value
    Set ie = CreateObject("InternetExplorer.Application")
    With ie
      .Visible = 1
      .navigate URL
      While .Busy Or .readyState <> 4
        DoEvents
      Wend
    End With
Dim Doc As HTMLDocument
    Set Doc = ie.document
    ActiveCell.Offset(0, 1).Value = "ERROR"
    name = Trim(Doc.getElementsByClassName("product-image-thumbs")(0).innerText)
    ActiveCell.Offset(0, 2).Value = name
    ActiveCell.Offset(0, 1).Value = "successful"
    ActiveCell.Offset(1, 0).Select
    ie.Quit
  Loop
End Sub
公共子宏1()
模糊的物体
将名称设置为字符串
直到IsEmpty(ActiveCell)为止
ActiveCell.Offset(0,1).Value=“正在运行”
URL=Selection.Value
设置ie=CreateObject(“InternetExplorer.Application”)
与ie
.Visible=1
.浏览网址
忙时或重新启动状态4
多芬特
温德
以
作为HTMLDocument的Dim Doc
Set Doc=ie.document
ActiveCell.Offset(0,1).Value=“错误”
name=Trim(Doc.getElementsByClassName(“产品图像拇指”)(0.innerText)
ActiveCell.Offset(0,2).Value=name
ActiveCell.Offset(0,1).Value=“成功”
ActiveCell.Offset(1,0)。选择
即退出
环
端接头
我的代码给空白单元格

另外,请建议我如何更快地运行此宏。。。。我有3000个url要处理

提前谢谢


根据注释,尝试以这种方式加速代码(未测试的代码)。
li
元素的内部文本是空字符串,因为它里面没有文本,所以有一个
image
元素,但没有文本。嗯

Public Sub macro1()
    Dim ie As Object
    Dim name As String
    Dim Doc As HTMLDocument

    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = 1

    Do Until IsEmpty(ActiveCell)
        ActiveCell.Offset(0, 1).Value = "RUNNING"
        url = Selection.Value
        ie.navigate url
        While ie.Busy Or ie.readyState <> 4
            DoEvents
        Wend
        Set Doc = ie.document
        ActiveCell.Offset(0, 1).Value = "ERROR"
        name = Trim(Doc.getElementsByClassName("product-image-thumbs")(0).innerText)
        ActiveCell.Offset(0, 2).Value = name
        ActiveCell.Offset(0, 1).Value = "successful"
        ActiveCell.Offset(1, 0).Select
    Loop

    ie.Quit
End Sub

编辑: 如果
li.product image thumbs
元素中有更多的
img
元素,那么您就有更多的可能性如何获得正确的
img

  • 获取位于
    li
    后面的
    img
“li[class~='product-image-thumbs']+img”

  • 按类名获取
    img
    中的
    li

“li[class~='product-image-thumbs']img[class~='etalage\u source\u image\u small']”

在文档上,您有一个前导空格
“product-image-thumbs”
,但在宏上检查
“product-image thumbs”
以加快运行速度,为什么不只创建一次IE对象并根据需要导航?@PhillipBeck也不使用前导空格…:(@Sgdva不起作用,如果我创建IE外部循环,还有其他建议吗?它起作用了,但它给了我另一个图像url,我忘了告诉我在“产品图像拇指”下的页面源中还有一个图像url),它正在给我第二张图片的url…编辑问题并添加您忘记的信息。我将根据此新信息编辑答案。设置img=imgs.Item(0)
Dim img, imgs As IHTMLDOMChildrenCollection, i
Set imgs = Doc.querySelectorAll("li[class~='product-image-thumbs']>img")

For i = 0 To imgs.Length - 1
    Set img = imgs.item(i)
    Debug.Print img.getAttribute("src")
Next