我想使用Excel vba从网站获取数据,但是我无法

时间:2018-10-11 18:15:53

标签: excel vba excel-vba web-scraping

Sub Galoplar()
    Sheets("Galop").Select
    Range("A1").Select
    Dim elem As Object, trow As Object
    Dim R&, C&, S$
    With New XMLHTTP60
        .Open "POST", "https://yenibeygir.com/at/getatdetaytab", False
        .setRequestHeader "content-type", "application/x-www-form-urlencoded; charset=UTF-8"
        .send "tab=galopTab&id=15673"
        S = .responseText
    End With

    With New HTMLDocument
        .body.innerHTML = S

        For Each elem In .getElementsByClassName("at_Galoplar")(0).Rows
            For Each trow In elem.Cells
                C = C + 1: Cells(R + 1, C) = trow.innerText
            Next trow
            C = 0: R = R + 1
        Next elem
    End With
End Sub

我从上面的代码从“ Web address”链接中获得“Galopları”。但是我无法通过以下代码获取“Yarışları”数据。

Sub Yarislar()
    Sheets("Yaris").Select
    Range("A1").Select
    Dim elem As Object, trow As Object
    Dim R&, C&, S$

    With New XMLHTTP60
        .Open "POST", "https://yenibeygir.com/at/getatdetaytab", False
        .setRequestHeader "content-type", "application/x-www-form-urlencoded; charset=UTF-8"
        .send "tab=yarisTab&id=15673"
        S = .responseText
    End With

    With New HTMLDocument
        .body.innerHTML = S

        For Each elem In .getElementsByClassName("at_Yarislar")(0).Rows
            For Each trow In elem.Cells
                C = C + 1: Cells(R + 1, C) = trow.innerText
            Next trow
            C = 0: R = R + 1
        Next elem
    End With
End Sub

我的问题是,我在哪里犯错?

如何使用vba代码在“ {Web address””链接上获取“ Son 1Yıl”数据?

1 个答案:

答案 0 :(得分:1)

“初始登录”选项卡没有其他选项卡执行的jquery启动的XHR事件。

您可以对着陆页发出GET请求,以按第一个标签的类名来获取表格。

' '

第二个问题(由于您无法发布):

32

编辑:在某些情况下,后期绑定剪贴板引用现在似乎有问题。这是通用的早期绑定方法,其中hTable是目标HTMLTable对象。

对于剪贴板早期绑定,请访问VBE>工具>参考> Microsoft-Forms 2.0对象库。

如果将UserForm添加到项目中,该库将自动添加。

132