|
IE提交数据效率较差,但内部运行的COOKIE等等会话验证却不需用使用者额外去处理,所以一切都变得很简单,非常适合初学者。
而VBA调用IE提交网页数据也有各种各样的方式,因人而异,有人非要模拟去单击网页的某个按钮,有人用getElementById去寻找某个标签。。但这些方式几乎都是所有方式中最低效的。如果非要用IE不可,也可以在IE之中寻找比较高效的。
下面以登录EP为例,分享二种方式:
1。利用HTML事件自动提交:
- Private Const sUser As String = "aaa" '改为你的用户名
- Private Const sPsw As String = "123456" '改为你密码
- Sub 登录EP()
- Dim objIE As Object
- Dim sCr As String
- Set objIE = CreateObject("InternetExplorer.Application")
- sCr = Chr(34)
- With objIE
- .Navigate "about:blank"
- With .Document
- .Open
- .write "<html><head><meta http-equiv=" & sCr & "Content-Type" & sCr & " content=" & sCr & "text/html; charset=gbk" & sCr & " /></head><body onload=" & sCr & "abc.submit();" & sCr & "><form name=" & sCr & "abc" & sCr & " action=" & sCr & " http://www.excelpx.com/member.php?mod=logging&action=login&loginsubmit=yes&infloat=yes" & sCr & " method=" & sCr & "post" & sCr & " ><input style=" & sCr & "width: 0px;height: 0px;" & sCr & " name=" & sCr & "username" & sCr & " type=" & sCr & "text" & sCr & " value=" & sCr & "" & sUser & "" & sCr & "/><input style=" & sCr & "width: 0px;height: 0px;" & sCr & " name=" & sCr & "password" & sCr & " type=" & sCr & "password" & sCr & " value=" & sCr & "" & sPsw & "" & sCr & " /></form></body></html>"
- .Charset = "gbk"
- .Close
- End With
- .Visible = True
- End With
- Set objIE = Nothing
- End Sub
复制代码 2。利IE对象的Navigate来发送
- Const sN = "aaa" '改为用户名
- Const sP = "123456" '密码
- Const URL = "http://www.excelpx.com/member.php?mod=logging&action=login&loginsubmit=yes&infloat=yes"
- Private Sub Command1_Click()
- Dim objIE As Object
- Dim btPost() As Byte
- Dim sHeaders As String
- Set objIE = CreateObject("InternetExplorer.Application")
- btPost = StrConv("fastloginfield=username&username=" & sN & "&password=" & sP & "&quickforward=yes&handlekey=ls&questionid=0&answer=", vbFromUnicode)
- sHeaders = "Content-Type: application/x-www-form-urlencoded" & vbCrLf
- With objIE
- .Navigate "about:blank"
- .Document.Charset = "gbk"
- .Navigate URL, 0, "", btPost, sHeaders
- .Visible = True
- End With
- Set objIE = Nothing
- End Sub
复制代码 示例:
登录EP.rar
(8.51 KB, 下载次数: 115)
|
评分
-
查看全部评分
|