AutoIt v3 _IETagNameGetCollection(...)faillure

时间:2016-01-15 13:36:56

标签: internet-explorer autoit

我试图在AutoIt中创建一个将在互联网上获取数据的脚本。 该脚本将Amazon URL作为输入,然后遍历所有页面,检索所呈现项目的ASIN编号并将其放入文件中。

以下是完整代码:

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <IE.au3>

Global $GUI = GUICreate("Amazon Parser", 350, 300, 200, 200)
Global $BoutonRun = GUICtrlCreateButton("Run", 30, 100, 125, 30)
Global $BoutonStop = GUICtrlCreateButton("Stop", 190, 100, 125, 30)
Global $TextEnter = GUICtrlCreateLabel("Enter the starting URL here : ", 90, 30, 300, 50)
Global $InputField = GUICtrlCreateInput("", 25, 50, 300, 35)
Global $Runing = false
Global $NbAsin = 0
Global $NbPages = 0
Global $OIE
Local  $Url = ""

GUISetState(@SW_SHOW)

While 1
   $nMsg = GUIGetMsg()
   Switch $nMsg
      Case $GUI_EVENT_CLOSE
         _IEQuit($OIE)
         Exit
      Case $BoutonRun
         $Url = GUICtrlRead($InputField)
         $Runing = true
         $NbAsin = 0
         $NbPages = 0
         $handle = FileOpen("./Res", $FO_OVERWRITE)
         FileClose($handle)
         $OIE = _IECreate($Url)
         _IELoadWait($OIE)
      Case $BoutonStop
         MsgBox(0, "Work Stopped !", "I collected " & $NbAsin & " ASIN" & @LF & "I went throught " & $NbPages & " pages of results.")
         _IEQuit($OIE)
         $Runing = false
   EndSwitch
   If $Runing == true Then
      $Url = GetPageData()
   EndIf
WEnd

Func GetPageData()
   $li = _IETagNameGetCollection($OIE, "li")
   $handle = FileOpen("./Res", $FO_APPEND)

   $NbPages = $NbPages + 1
   For $l In $li
      If StringInStr($l.className, "s-result-item") > 0 And StringInStr($l.className, "celwidget") > 0 Then
         FileWriteLine($handle, $l.GetAttribute("data-asin"))
         $NbAsin = $NbAsin + 1
      EndIf
   Next

   $ret = _IELinkClickByText($OIE, "Next Page", 0, 0)
   If $ret == 0 Then
      $Runing = false
      _IEQuit($OIE)
      MsgBox(0, "Work Done !", "I successfully collected " & $NbAsin & " ASIN" & @LF & "I went throught " & $NbPages & " pages of results." & @LF & "The result can be found in the 'res' file")
   EndIf
   FileClose($handle)
EndFunc

正如你所看到的那样,那里没什么复杂的。这个脚本(几乎)完美地工作,它遍历页面并获取ASIN。

但随后在一个随机页面(有时是第9页,有时是第18页,它不一致......),脚本因以下错误而无故失败:

(1811) : ==> The requested action with this object has failed.:
Return SetError($_IESTATUS_Success, $oTemp.GetElementsByTagName($sTagName).length, $oTemp.GetElementsByTagName($sTagName))
Return SetError($_IESTATUS_Success, $oTemp^ ERROR

IE.au3库中发生此错误,因此严格终止程序而不发出警告。

知道如何克服这个问题吗?或者,如果你有另一个可以完成这项工作的功能,我可以接受它!

1 个答案:

答案 0 :(得分:1)

_IELoadWait之后您遗漏了_IELinkClickByText

_IETagNameGetCollection失败,因为页面未准备好(已加载)。

_IELinkClickByText默认等待,但出于某种原因,您通过将第三个参数设置为0来关闭它。

您应该始终添加一个错误处理程序,以避免致命的IE错误。

以下是您的代码,其中包含一些更改:

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <IE.au3>

Global $GUI = GUICreate("Amazon Parser", 350, 300, 200, 200)
Global $BoutonRun = GUICtrlCreateButton("Run", 30, 100, 125, 30)
Global $BoutonStop = GUICtrlCreateButton("Stop", 190, 100, 125, 30)
Global $TextEnter = GUICtrlCreateLabel("Enter the starting URL here : ", 90, 30, 300, 50)
Global $InputField = GUICtrlCreateInput("", 25, 50, 300, 35)
Global $Runing = false
Global $NbAsin = 0
Global $NbPages = 0
Global $OIE
Local  $Url = ""


_IEErrorNotify(True)
$oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")

GUISetState(@SW_SHOW);  http://www.amazon.com/s//www.amazon.com/s?marketplaceID=ATVPDKIKX0DER&me=A3QENM74IQHUW5&merchant=A3QENM74IQHUW5

While 1
   $nMsg = GUIGetMsg()
   Switch $nMsg
      Case $GUI_EVENT_CLOSE
         _IEQuit($OIE)
         Exit
      Case $BoutonRun
         $Url = GUICtrlRead($InputField)
         $Runing = true
         $NbAsin = 0
         $NbPages = 0
         $handle = FileOpen("./Res", $FO_OVERWRITE)
         FileClose($handle)
         $OIE = _IECreate($Url)
;~          _IELoadWait($OIE); not needed because _IECreate waits on default
      Case $BoutonStop
         MsgBox(0, "Work Stopped !", "I collected " & $NbAsin & " ASIN" & @LF & "I went throught " & $NbPages & " pages of results.")
         _IEQuit($OIE)
         $Runing = false
   EndSwitch
   If $Runing == true Then
      GetPageData()
   EndIf
WEnd

Func GetPageData()
   $handle = FileOpen("./Res", $FO_APPEND)

   $li = _IETagNameGetCollection($OIE, "li")
   If Not @error Then


       $NbPages = $NbPages + 1
       For $l In $li
          If StringInStr($l.className, "s-result-item") And StringInStr($l.className, "celwidget") Then
             FileWriteLine($handle, $l.GetAttribute("data-asin"))
             $NbAsin = $NbAsin + 1
          EndIf
       Next

    EndIf

   $ret = _IELinkClickByText($OIE, "Next Page", 0, 1) ; changed last parameter to 1 in order to wait for load.
   If @error Then
      $Runing = false
      _IEQuit($OIE)
      MsgBox(0, "Work Done !", "I successfully collected " & $NbAsin & " ASIN" & @LF & "I went throught " & $NbPages & " pages of results." & @LF & "The result can be found in the 'res' file")
   EndIf
   FileClose($handle)

EndFunc



Func _ErrFunc($oError)

    ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _
            @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _
            @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
            @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _
            @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _
            @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
            @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
            @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
            @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
            @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF)

EndFunc   ;==>_ErrFunc