2016-12-09 52 views
0

我一直在尝试使用VBA使Excel中的某些工作更容易,到目前为止,它一直很棒..但是目前我有这两个元素需要从HTML中获取我不能为我的生活文档弄清楚:在VBA中使用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 

什么我要找的是供应商的变量(所谓的“品牌”下方),以及作为Image链接,下面是显示值的HTML代码片段:

<meta itemprop="brand" content="Intel" /> 
    <meta itemprop="image" content="http://ecx.images-amazon.com/images/I/510BosCAMcL.jpg" /> 

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

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

(PS的HTML来源于此链接:https://pcpartpicker.com/product/W67wrH/intel-cpu-bx80646g1820

回答

1

既然你是想通过这些让所有的meta元素可以循环,并通过检查itemProp串

抓住品牌和形象

编辑:你似乎已经从你的问题中删除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 
+0

谢谢!我看看这个(我删除了metaElemets,因为我不确定它是否会被使用) –

+0

完美地工作,感谢您的帮助! –