2017-08-15 293 views
0

我是一個初學者使用excel vba進行網頁抓取,需要一些幫助。vba通過xpath引用html元素

我想引用一個元素。如果有一個ID,那麼我可以使用getElementByID,但有時沒有ID。我可以使用getElementByClassName,但有時同一類的元素太多。

有什麼方法可以通過xpath引用元素嗎?

(我不能發佈實際的網站因爲有個人信息,以便讓我們說這是HTML)

<!DOCTYPE html> 
<html> 
<body> 

<a href="https://google.com">Link</a> 

</body> 
</html> 

是有什麼樣ie.document.getElementByXPath。(/ HTML /體/一)。點擊? 我在網上搜索過,似乎無法找到關於該主題的任何內容。

+0

我想你可能指的是選擇一個元素的屬性。例如,獲取'href =='google.com''的元素。我對麼? –

+0

IF(大)如果HTML是有效的XHTML,那麼它是有效的XML,並且可以由MSXML庫處理,MSXML庫提供了您想要的功能。 –

+0

不是xpath,而是'queryselector [all]'接近你的要求:https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector –

回答

0

這並不意味着是一個答案

這裏是一對夫婦潛艇,可能會給你一些想法

Sub google() 

    ' add reference: Microsoft XML v6.0 

    Const url = "https://www.google.co.in" 

    Dim http As New XMLHTTP60 
    Dim html As New HTMLDocument 

    http.Open "GET", url, False 
    http.Send 
    html.body.innerHTML = http.responseText 

    Dim elem As Object 
    Set elem = html.getElementsByClassName("ctr-p")    ' HTMLElementCollection 
    Debug.Print elem.Length 

    Set elem = html.getElementsByClassName("ctr-p")("viewport") ' HTMLDivElement <div class="ctr-p" id="viewport"> 
    Debug.Print elem.Children.Length 


    Dim aaa As Object 
    Set aaa = elem.getElementsByTagName("div")("hplogo")   ' HTMLDivElement 
    Debug.Print aaa.Children.Length 
    Debug.Print aaa.outerHTML 

End Sub 

' add references Microsoft HTML Object Library 
'     Microsoft Internet Controls 

Sub ieGoogle() 

    Const url = "https://www.google.co.in" 

    Dim iE As InternetExplorer 
    Set iE = New InternetExplorer 

    iE.Navigate url 
    iE.Visible = True 

    Do While iE.ReadyState <> 4: DoEvents: Loop 

    Dim doc As HTMLDocument 
    Set doc = iE.Document 

    Debug.Print doc.ChildNodes.Length       ' DOMChildrenCollection 
    Debug.Print doc.ChildNodes(1).ChildNodes.Item(0).nodeName ' HEAD 
    Debug.Print doc.ChildNodes(1).ChildNodes.Item(1).nodeName ' BODY 


    ' for querySelector arguments see: https://www.w3schools.com/cssref/css_selectors.asp 

    Dim elm As HTMLInputElement 
    Set elm = doc.querySelector("*")      ' all elements 

    Debug.Print Left(elm.outerHTML, 40) 
    Set elm = doc.querySelector("div.ctr-p#viewport") ' <div class="ctr-p" id="viewport"> 
    Debug.Print Left(elm.outerHTML, 40) 
    Set elm = doc.querySelector(".ctr-p#viewport")  ' <div class="ctr-p" id="viewport"> 

    Debug.Print Left(elm.outerHTML, 40) 
    Debug.Print elm.ChildNodes.Length 
    Debug.Print elm.Children.Length 


    Set elm = doc.querySelector("#viewport")    ' id="viewport" 
    Debug.Print Left(elm.outerHTML, 40) 


    Debug.Print elm.ID 


    Dim elem As HTMLInputElement 
    Set elem = doc.getElementsByClassName("ctr-p")("viewport") 



    Debug.Print elem.Children.Length 

    Dim aaa As Object 
    Set aaa = elem.getElementsByTagName("div")("hplogo") 
    Debug.Print aaa.Children.Length 
    Debug.Print aaa.outerHTML 

    iE.Quit 
    Set iE = Nothing 
End Sub 
相關問題