internet-explorer - VBA/VB 脚本自动网页抓取循环/IE 登录

标签 internet-explorer vbscript

我一直在尝试编写一个脚本,该脚本将在渲染网站上抓取某个字段/标签。该网站使用我在 Excel 中的列列表中的搜索参数呈现。大约 20 项将增长。在研究了如何使用 vbscripts 进行网络抓取之后,我遇到的问题是如何在不中断的情况下执行此操作 20 次。这是我的代码。
Excel 列

1492565
1528417
1529041
1530688
1492038
1492319
1492972
1508824
1513351
1514724
1514750
1518526
1520627
1520706
1520979
1523367
1523563
脚本:
主子(从 excel 输入字段获取用户/传递,在特定列上的行设置循环。只吐回一个 msgbox,直到我可以让循环工作。然后我将它输出到另一列
Sub WebScraper()
    'itg on mainWS start row 6, column 5
    'itg status column column 19
    'declare variables
    Dim url As String
    Dim ITGNUMBER As Long
    Dim user As String
    Dim pwd As String
    
    'set variables
    url = "https://website/itg/web/knta/crt/RequestDetail.jsp?REQUEST_ID="
    Set oMainWS = ActiveWorkbook.Worksheets("MainWS")
    Set oITGStatusWS = ActiveWorkbook.Worksheets("ITGStatus")
    user = ""
    pwd = ""
    
    user = oITGStatusWS.ITGusername.Value
    pwd = oITGStatusWS.ITGpassword.Value
    
    If user = "" Or pwd = "" Then
        MsgBox ("You must enter username/password before continuing")
        Exit Sub
    End If
    
    'log in
    Set objIE = FirstIEConnect(user, pwd)
    
    'start row is 6
    RowCounter = 58
    ColumnCounter = 5
    ITGStatusColumn = 16
    Do Until IsEmpty(oMainWS.Cells(RowCounter, 5).Value)
        'get ITG number
        currentITGNumber = oMainWS.Cells(RowCounter, 5).Value
        MsgBox (currentITGNumber)
        'get remote status
        currentITGStatus = getITGStatusFunction(objIE.Application, Str(currentITGNumber))
        MsgBox (currentITGStatus)
        
        'paste into column 19
        'oMainWS.Cells(RowCounter, 19).Value = currentITGStatus
        
        'increment counter
        RowCounter = RowCounter + 1
        currentITGStatus = ""
        currentITGNumber = ""
    Loop
    quitIE (objIE.Application)
End Sub
Sub to quitIE 对象清理,有一个 javascript 函数来注销用户。
Sub quitIE(obj As Object)
    obj.Navigate ("javascript: closeChildWindowsAndLogout();")
    obj.Quit
End Sub
从谷歌得到这个子,用于等待 IE 对象准备就绪。这实际上在循环中失败了很多。 On Do While IE.Busy:Loop。就挂了。
Sub Wait(obj As Object)
    Do While obj.Busy: Loop
    Do While obj.readyState <> 4:   Loop
     Application.Wait (Now + TimeValue("0:00:01"))
End Sub
该网站需要登录,用户/密码来自第一个子。这个子创建IE对象,导航到登录页面并将用户/密码插入Document.logon.UserNameDocument.logon.Password .最后提交。
Function FirstIEConnect(user As String, pwd As String)
    loginURL = "https://website/Logon.jsp"
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = False
    IE.Navigate loginURL
    Wait (IE.Application)
    With IE.Document.logon
      .UserName.Value = user
      .Password.Value = pwd
      .submit
    End With

    Set FirstIEConnect = IE
End Function
这是实际的网页抓取功能。要求用户从上面登录到 IE 对象。在 url GET 请求中输入 num 以呈现特定页面。终于抢到responseText基于元素ID
Function getITGStatusFunction(obj, num)
    On Error Resume Next
    'set url and num
    url = "https://website/RequestDetail.jsp?REQUEST_ID=" & num
  
    obj.Navigate url
    Wait (obj.Application)
    responseText = obj.Document.getElementByID("DRIVEN_STATUS_ID").innerHTML
   
    getStatusFunction = responseText
    
End Function
再次,
问题是当我尝试从不同的 subs 和函数传递 IE 对象时,我不断收到对象错误。
期待:
我希望脚本循环遍历 excel 中包含唯一数字的列信息。一一获取这些数字,并将它们一一附加到搜索 URL 中。页面加载后抓取 ElementID(DRIVEN_STATUS_ID) .最终获取该值并将其输出到另一列。

最佳答案

Wait (IE)路过IE.Name而不是对象。要传递对象,请使用 IE.Application .

Tim Williams 的评论也是正确的,省略括号也会导致对象被传递。所以Wait IE而不是 Wait (IE)也会起作用。不过我觉得用Wait IE.Application比较安全只是为了明确。显然,你决定。

如果您要更新等待功能,它仍然无法解决问题,但问题出在哪里会更明显。Function Wait(IE as object)当您尝试调用函数声明需要一个对象时失败。

这一点应该很明显,但不要通过IE , 通 IE.Application在所有情况下,除非您实际上只想要对象的名称。

顺便说一句,Set variable = IE行很好,因为关键字 Set明确表示您想要该对象。

关于internet-explorer - VBA/VB 脚本自动网页抓取循环/IE 登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12217761/

相关文章:

VBA 网页抓取 : Difference between internet explorer and XMLHTTP request

html - IE float 权限问题

javascript - 在 IE 中 <a> 中垂直和水平居中 <img> 的麻烦

jQuery fadeIn 在 IE7 中留下未消除锯齿的文本

asp-classic - 使用 VBScript 和 ASP Classic 的服务器时间

vbscript - 批处理文件转换为 vbscript

performance - Angular2 在 IE 中运行缓慢并且使用 core-js

batch-file - 如何在 VBS 中创建循环?

vba - 在 VBA/VBS 中退出 while 循环

.net - 是否可以在 VBScript 中获取 .Net 字符串对象实例?