Vba code to retrieve data from website

I am new to this site and to VBA programming. I encountered a problem when I have to retrieve data from this page . I need a hyperlink url Check Rates 10. Can anyone help me with this problem.

I made 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 get the button text, but want to have a class value. I think that I am very close to the result, but somehow I can’t achieve the goal ... can someone help me ...

Hello,

+4
source share
2 answers

I think this is what you are looking for:

(Code slightly modified 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

URL- . , !

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

Source: https://habr.com/ru/post/1526727/


All Articles