使用Excel将信息提交给网站表单并parsing结果
下午好,
我试图设置一个自动化一个内部网站的报告的手段。 我在大学教书,需要通过大学提供的界面从教师可访问的数据库中提取一些数据。 所以,我试图访问我有权查看的资料,需要login才能看到,但不想重复这些步骤数百次。
我的想法是:
1)使用IE凭证login学院提供的界面(所以系统知道我是授权的)。
2)提供优秀的学生身份证号码列表,我试图跟踪
3)使用VBA遍历每个ID,执行以下步骤:
- 从Excel列表中inputID号码到适当的表格文本框中
- 点击“提交”button
- 点击结果页面上的辅助“提交”button
- 复制出现的HTML文本页面,并parsing出我需要的信息
- 移到下一个数字并重复
我已经尝试了一些我见过的其他选项(如VBAinput数据在线和提交表单 ),但是在语法上出现错误。
我试图引用的网站上的HTML编码的相关部分是:
<FORM ACTION="action" METHOD="POST" NAME="idinputform"> Enter Number Here: <INPUT TYPE="number" NAME="ID_NUM" SIZE="9" MAXLENGTH="9"> <INPUT TYPE="hidden" NAME="refresh_proc" VALUE="menu_1"> <INPUT TYPE="submit" VALUE="Submit"> <INPUT TYPE="reset" VALUE="Reset"> </FORM>
我在Excel中得到的是:
Sub QueryInfo() Application.ScreenUpdating = False Set IE = CreateObject("InternetExplorer.Application") IE.Visible = True IE.Navigate "website address" IE.Document.idinputform.number.Value = "000000000" Application.ScreenUpdating = True End Sub
所以,我先打开一个IE窗口并login界面,然后启动VBA。 应用程序运行,打开一个新的IE窗口,点击网站(保留我的login权限,从第一个),然后崩溃与未指定的错误。
这似乎应该是直截了当的,但我只是没有看到它。
谢谢! -G-
当处理集合(多个对象)时,我发现最好是遍历可用的对象,随时testing每个对象。
Sub QueryInfo() Dim ie As Object, iFRM As Long, iNPT As Long 'Application.ScreenUpdating = False 'uncomment this once it is working Set ie = CreateObject("InternetExplorer.Application") ie.Visible = True ie.Navigate "website address" 'wait untli the page loads Do While ie.busy Or ie.readyState <> 4 'READYSTATE_COMPLETE = 4 DoEvents Loop With ie.document.body For iFRM = 0 To .getElementsByTagName("form").Length - 1 If LCase(.getElementsByTagName("form")(iFRM).Name) = "idinputform" Then With .getElementsByTagName("form")(iFRM) For iNPT = 0 To .getElementsByTagName("input").Length - 1 Select Case LCase(.getElementsByTagName("input")(iNPT).Name) Case "id_num" .getElementsByTagName("input")(iNPT).Value = 123 Case "refresh_proc" .getElementsByTagName("input")(iNPT).Value = "menu_2" End Select Next iNPT .submit '<~~ submit the form Do While ie.busy Or ie.readyState <> 4: DoEvents: Loop Exit For End With Exit For End If Next iFRM End With With ie.document.body 'should be at the form's destination End With Application.ScreenUpdating = True End Sub
页面上可能有多种forms。 通过每一个,直到你find了正确的名字。 里面的表单定义,是多个input元素; 循环遍历每一个,并根据需要应用参数。 完成后,提交表单并退出循环。