使用 VBA 和 Excel 检索 epay.info 余额

Retrieve epay.info Balance with VBA and Excel

有问题的网站在访问数据之前有一个登录表单,但它没有提供任何 API。 一些想法或建议? 这是登录页面代码的摘录 (http://epay.info/Login/):

<form enctype="application/x-www-form-urlencoded" class="form-signin" action="" method="post" style="margin-top: 30px;">
    <h2 class="form-signin-heading">ePay.info Login</h2>
    <div class="login-wrap">
    <div class="form-group">
        <input style="margin-right: 0px; padding-right: 0px;" name="username" id="username" value="" placeholder="Wallet addresses, Username or E-mail address" class="form-control input-lg" type="text"><img title="Lunghezza massima del campo sconosciuta" style="position: relative; left: 304px; top: -33px; z-index: 999; cursor: pointer; vertical-align: bottom; border: 0px none; width: 14px; height: 19px; display: inline;" class="ife_marker" src="chrome://informenter/skin/marker.png" id="username_ife_marker_0">
    </div>
    <div class="form-group">
        <input style="margin-right: 0px; padding-right: 0px;" name="password" id="password" value="" placeholder="Password (only if secured)" class="form-control input-lg" type="password"><img title="Lunghezza massima del campo sconosciuta" style="position: relative; left: 304px; top: -33px; z-index: 999; cursor: pointer; vertical-align: bottom; border: 0px none; width: 14px; height: 19px; display: inline;" class="ife_marker" src="chrome://informenter/skin/marker.png" id="password_ife_marker_1">
    </div>
    <div class="row">
        <button class="btn btn-lg btn-login btn-block" name="login" type="submit">Login and check stats</button>
    </div>

如果您为这两行输入正确的值,此代码应该会让您登录:

username.Value = "Test"
password.Value = "Test"

Alt+F11 打开 VB 编辑器并插入一个新的标准代码模块。粘贴以下代码:

Option Explicit

Private Sub LoginToEpay()

    Dim IE As Object
    Dim username As Object
    Dim password As Object
    Dim objCollection As Object
    Dim i As Integer

    Set IE = CreateObject("InternetExplorer.Application")

    IE.Visible = True

    'Navigate to the website
    IE.navigate URL:="http://epay.info/Login/"

    'Wait for page to load
    Do While IE.Busy Or IE.readyState <> READYSTATE_COMPLETE
        Application.Wait DateAdd("s", 1, Now)
    Loop

    'Username and password elements have IDs
    Set username = IE.document.getElementById("username")
    Set password = IE.document.getElementById("password")

    'Login button doesn't have an ID so we must use name
    'This returns an array of items that have the name
    'login
    Set loginBtn = IE.document.getElementsByName("login")

    username.Value = "Test"
    password.Value = "Test"

    While i < objCollection.Length
        If objCollection(i).Type = "submit" Then objCollection(i).Click
        i = i + 1
    Wend

End Sub

希望其中大部分是不言自明的。最令人困惑的部分是 getElementByIdgetElementsByName。第一个 return 是基于 HTML 中的 id 的单个元素,而第二个 return 是包含您传入的任何名称的对象数组。我可以使用 getElementById 因为为用户名和密码字段提供了 ID。

没有为登录按钮提供 ID,因此要获得该按钮,我必须 return 一个包含名称为 "login" 的所有对象的数组(在这个特定示例中,有只有一个)。然后,我循环浏览 While..Wend 循环中的所有按钮,然后按下显示提交的按钮。

我本可以不包含 While..Wend 循环并简单地说:

objCollection(0).Click

但我认为看到更好的做法对您最有利。

这至少会让您登录。