AutoIt v3 _IETagNameGetCollection(...) 失败
AutoIt v3 _IETagNameGetCollection(...) faillure
我正在尝试在 AutoIt 中创建一个脚本,它将在 Internet 上获取数据。
该脚本将亚马逊 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库中,所以它严格终止了程序,没有警告。
知道如何克服这个问题吗?或者,如果您有其他功能可以完成这项工作,我愿意接受!
您在 _IELinkClickByText
之后缺少一个 _IELoadWait
。
_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
我正在尝试在 AutoIt 中创建一个脚本,它将在 Internet 上获取数据。 该脚本将亚马逊 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库中,所以它严格终止了程序,没有警告。
知道如何克服这个问题吗?或者,如果您有其他功能可以完成这项工作,我愿意接受!
您在 _IELinkClickByText
之后缺少一个 _IELoadWait
。
_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