从网站上获取一个号码

时间:2018-01-26 17:49:56

标签: vba excel-vba dom web-scraping children

我试图从网站上获取欧元金额字符串的数字部分。我尝试了不同的东西,通常都有效。这是我最好的3次尝试。什么都没有用

enter image description here

我的第一次尝试:

Sub gsellor() 
    Dim objIE As InternetExplorer 
    Dim aEle As HTMLLinkElement 
    Dim HTMLtags As IHTMLElementCollection
    Dim HTMLtag As IHTMLElement
    Dim HTMLPreisTag As IHTMLElement

    Set objIE = New InternetExplorer
    objIE.Visible = True

    objIE.navigate "https://www.gdax.com/trade/LTC-EUR"
    Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop


    Set HTMLPreisTag = objIE.document.getElementById("page_content").Children(0).Children(1).Children(0).Children(1).Children(0)
    Debug.Print HTMLPreisTag.className
    Debug.Print HTMLPreisTag.Children(1).Children(0).Children(0).innerText     ' gets stuck right here



    objIE.Quit

End Sub

即时窗口显示:

MarketInfo_market-info_3lkUj

我的第二次尝试:

Sub gsellor()  
    Dim objIE As InternetExplorer 
    Dim aEle As HTMLLinkElement 
    Dim HTMLtags As IHTMLElementCollection
    Dim HTMLtag As IHTMLElement
    Dim HTMLPreisTag As IHTMLElement

    Set objIE = New InternetExplorer
    objIE.Visible = True

    objIE.navigate "https://www.gdax.com/trade/LTC-EUR"
    Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop


    Set HTMLtags = objIE.document.getElementsByTagName("span")
    Debug.Print HTMLtags.Length
    For Each HTMLtag In HTMLtags
        Debug.Print "name: " & HTMLtag.className
        Debug.Print "text: " & HTMLtag.innerText

    Next HTMLtag


    objIE.Quit

End Sub

即时窗口显示:

 3
name: 
text: 
name: AccountPanel_account-panel-user-name_2TNIL
text: 
name: sr-only
text: Toggle navigation

我的第三次尝试:

Sub gsellor()   
    Dim objIE As InternetExplorer 
    Dim aEle As HTMLLinkElement 
    Dim HTMLtags As IHTMLElementCollection
    Dim HTMLtag As IHTMLElement
    Dim HTMLPreisTag As IHTMLElement

    Set objIE = New InternetExplorer
    objIE.Visible = True

    objIE.navigate "https://www.gdax.com/trade/LTC-EUR"
    Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop


    Set HTMLtags = objIE.document.getElementsByClassName("MarketInfo_market-num_1lAXs")
    Debug.Print HTMLtags.Length
    For Each HTMLtag In HTMLtags
        Debug.Print "name: " & HTMLtag.className
        Debug.Print "text: " & HTMLtag.innerText

    Next HTMLtag


    objIE.Quit

End Sub

即时窗口:

0 

1 个答案:

答案 0 :(得分:1)

获取使浏览器等待的内容,直到内容不是什么为止。这是实现相同目的的另一种方式:

Sub gsellor()
    Dim ObjIE As New InternetExplorer
    Dim Ohtml As HTMLDocument
    Dim item_price As Object

    With ObjIE
        .Visible = True
        .navigate "https://www.gdax.com/trade/LTC-EUR"
        Do Until .readyState = READYSTATE_COMPLETE: Loop
        Set Ohtml = .document
    End With

    Do
        Set item_price = Ohtml.getElementsByClassName("MarketInfo_market-num_1lAXs")(0)
        DoEvents
    Loop While item_price Is Nothing

    MsgBox item_price.innerText

    ObjIE.Quit
End Sub