vba 从网站获取数据的vba代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21751880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:46:12  来源:igfitidea点击:

vba code to fetch data from website

excel-vbawebweb-scrapingvbaexcel

提问by user3305327

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 10button. Can anyone help me with this problem.

我是这个网站和 VBA 编程的新手。我遇到了一个问题,我必须从这个页面获取数据。我需要有Check Rates 10按钮的超链接网址。谁能帮我解决这个问题。

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,

问候,

采纳答案by ARich

I think this is what you're looking for:

我认为这就是你要找的:

(Code modified slightly from Kyle's answer here)

(代码从Kyle的回答稍微修改这里

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!

此代码将 URL 打印到即时窗口。希望有帮助!

回答by ron

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