2010-05-21 61 views
1

有沒有辦法通過VBScript遍歷HTML網頁上的所有元素?例如,我想獲得一個HTML網頁上的所有對象的信息:http://www.echoecho.com/html.htm有沒有辦法通過VBScript遍歷HTML網頁上的所有元素?

這裏是我創建的腳本,謝謝:

Dim objIE 
Dim IEObject 
Dim Info 
Info = 「」 

Set objIE = CreateObject("InternetExplorer.Application") 
objIE.Visible = True 
objIE.Navigate "http://www.echoecho.com/html.htm" 

Do While objIE.Busy Or (objIE.READYSTATE <> 4) 
    Wscript.Sleep 100 
Loop 
WScript.Sleep 50 

‘ Can I use objIE.Document.all.Item.length to count all elements on the webpage? 
For i=1 to objIE.Document.all.Item.length then 

    IEObject = objIE.Document..??????... item (i-1)…??? 
    Info = Info & vbCrLf & i & IEObject.id & 「-「IEObject.title & 「-「 & IEObject.name & 「-「 & ….etc. 

Next 

Wscript.Echo Info 

Set IEObject = Nothing 
Set objIE = Nothing 

回答

0

你的問題是有點模糊,但我相信這將這樣的伎倆:

Dim objIE, IEObject, Info, all, hasOwnProperty 
Info = "" 

Set objIE = CreateObject("InternetExplorer.Application") 
objIE.Visible = True 
objIE.Navigate "http://www.echoecho.com/html.htm" 

Do While objIE.Busy Or (objIE.READYSTATE <> 4) 
Wscript.Sleep 100 
Loop 
WScript.Sleep 50 

Set hasOwnProperty = objIE.Document.ParentWindow.Object.prototype.hasOwnProperty 

' Can I use objIE.Document.all.Item.length to count all elements on the webpage? 
Set all = objIE.Document.all 
For i = 0 To all.Item.Length - 1 
Set IEObject = all.Item(i) 

'If this is not the first item, place this data on a new line. 
If i > 0 Then Info = Info & vbCrLf 

' Number each line. 
Info = Info & i + 1 & ". " 

' Specify the ID if it is given. 
If hasOwnProperty.call(IEObject, "id") Then 
    Info = Info & IEObject.id 
Else 
    Info = Info & "[NO ID]" 
End If 

' Specify the title if it is given. 
If hasOwnProperty.call(IEObject, "title") Then 
    Info = Info & "-" & IEObject.title 
Else 
    Info = Info & "-[NO TITLE]" 
End If 

' Specify the name if it is given. 
If hasOwnProperty.call(IEObject, "name") Then 
    Info = Info & "-" & IEObject.name 
Else 
    Info = Info & "-[NO NAME]" 
End If 
Next 

Wscript.Echo Info 

Set IEObject = Nothing 
Set objIE = Nothing 

你會發現,我正在使用順序hasOwnProperty函數來檢查指定的屬性是否真正存在給定元素。沒有這個檢查,VBScript會發生錯誤,但不會在JScript中發生。

相關問題