2014-02-13 55 views
4

我是本網站的新手,也是VBA編程的新手。我陷入了一個問題,我必須從this page獲取數據。我需要有超鏈接網址Check Rates 10按鈕。任何人都可以幫助我解決這個問題。從網站獲取數據的vba代碼

我也做了下面的代碼:

Sub GetData() 

Dim IE As New InternetExplorer 
IE.navigate "http://www.kieskeurig.nl/zoeken/index.html?q=4960999543345" 
IE.Visible = False 

Do 
    DoEvents 
Loop Until IE.readyState = READYSTATE_COMPLETE 

Application.Wait (Now() + TimeValue("00:00:016")) ' For internal page refresh or loading 
Dim doc As HTMLDocument 'variable for document or data which need to be extracted out of webpage 
Set doc = IE.document 
Dim dd As Variant 
dd = doc.getElementsByClassName("lgn")(0).outerHtml 
'Range("a1").Value = dd 
MsgBox dd 

End Sub 

在這我收到按鈕的文本,但我想有這個類的值。我覺得我很接近的結果,但不知何故着達到目標...誰能幫我...

問候,

回答

0

我認爲這是你在找什麼:

Sub Test() 
'Must have the Microsoft HTML Object Library reference enabled 
Dim oHtml As HTMLDocument 
Dim oElement As Object 
Dim link As String 

Set oHtml = New HTMLDocument 

With CreateObject("WINHTTP.WinHTTPRequest.5.1") 
    .Open "GET", "http://www.kieskeurig.nl/zoeken/index.html?q=4960999543345", False 
    .Send 
    oHtml.Body.innerHTML = .responseText 
End With 

If InStr(1, oHtml.getElementsByClassName("lgn")(0).innerText, "Bekijk 10 prijzen") > 0 Then 
    link = Mid(oHtml.getElementsByClassName("lgn")(0).href, 7) 
    Debug.Print "http://www.kieskeurig.nl" & link 
End If 

End Sub 

此代碼打印的URL即時窗口(代碼略凱爾的答案here修改)。希望有所幫助!

+0

感謝你們兩個......它真的爲我工作!謝謝大家 – user3305327

0

這對我的作品......

Sub GetData() 
    Set IE = CreateObject("InternetExplorer.Application") 
    my_url = "http://www.kieskeurig.nl/zoeken/index.html?q=4960999543345" 

    With IE 
     .Visible = True 
     .navigate my_url 
     .Top = 50 
     .Left = 530 
     .Height = 400 
     .Width = 400 

    Do Until Not IE.Busy And IE.readyState = 4 
     DoEvents 
    Loop 

    End With 

    Application.Wait (Now() + TimeValue("00:00:016")) ' For internal page refresh or loading 

    Set Results = IE.document.getElementsByTagName("a") 
    For Each itm In Results 
     If itm.classname = "lgn" Then 
      dd = itm.getAttribute("href") 
      Exit For 
     End If 
    Next 

' if you wnat to click the link 
    itm.Click 

' otherwise 
    'Range("a1").Value = dd 
    MsgBox dd 
End Sub 
+0

大家好!我在這個查詢中遇到一個問題,而我通過代碼循環有一些頁面「lgn」類不可用,因此我需要打印「不可用」但出現錯誤。 ... – user3305327

+0

嗨,我已通過以下代碼解決此問題 如果oHtml.getElementsByClassName(「lgn」)。Length> 0然後 如果InStr(1,oHtml.getElementsByClassName(「lgn」)(0).innerText, 「0」然後 link = Mid(oHtml.getElementsByClassName(「lgn」)(0).href,7) Range(「f」&i).Value =「http://www.kieskeurig .nl「&link End If Else Range(」f「&i).Value =」NA「 End If Next i 謝謝, – user3305327