vba code to fetch data from website

2020-04-09 23:50发布

I am a newbie in this website and in VBA programming as well. I am stuck into a problem where I have to fetch the data from this page. I need to have the hyperlink url of Check Rates 10 button. Can anyone help me with this problem.

I have done the following code:

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

In which I am getting text of the button but I want to have the value of the class. I think I am very close to the result but somehow cant reach to the goal...can anyone please help me...

Regards,

2条回答
放荡不羁爱自由
2楼-- · 2020-04-10 00:35

This works for me...

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
查看更多
来,给爷笑一个
3楼-- · 2020-04-10 00:38

I think this is what you're looking for:

(Code modified slightly from Kyle's answer here)

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

This code prints the URL to the immediate window. Hope that helps!

查看更多
登录 后发表回答