使用VBA和Excel检索epay.info平衡

该网站在访问数据之前获得了一个login表单,而且没有提供任何API。 一些想法或build议? 以下是login页面的代码摘录( 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> 

如果您为这两行input正确的值,此代码应该会login您:

 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 。 第一个返回基于HTML中的id的单个元素,而第二个返回包含您传入的任何名称的对象数组。我可以使用getElementById因为为用户名和密码字段提供了ids。

没有为loginbutton提供的id,所以要获得该button,我不得不返回一个名为“login”(在这个特定的例子中,只有一个)的所有对象的数组。 然后,我遍历While..Wend循环中的所有button,并按下提交的button。

我本来可以不包括While..Wend循环,只是说:

 objCollection(0).Click 

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

这至less会让你login。