中
我正在尝试编写一个vbscript,该脚本从具有class
属性的<img>
标记的src
属性中提取值作为此cs-poster-big
这是我迄今为止尝试过的代码,
'Initializing object with Internet Explorer Application
set IE = WScript.CreateObject("InternetExplorer.Application", "IE_")
'setting properties of Internet Explorer to the newly create object
with IE
.Visible = 0
.navigate "http://www.roku.com/channels/#!details/12" 'INSERT WEBPAGE HERE
end with
'waiting for IE to load the page
'tried using IE.busy or IE.readyState <> 4 also
while IE.busy
wScript.sleep 500
wend
wScript.sleep 500
'getting all image tags from the webpage
Set imgTags = IE.document.getElementsByTagName("IMG")
'iterating through the image tags to find the one with the class name specified
For Each imgTag In imgTags
'tried imgTag.className also
If imgTag.getAttribute("class") = "cs-poster-big" Then MsgBox "src is " & imgTag.src
next
IE.quit
set IE= Nothing
MsgBox "End of script"
它没有显示任何值,但您可以在此处查看页面的来源,您可以看到它有一个带有class
cs-poster-big
的<img>
标签
我不明白为什么它没有显示在我的脚本
Do While IE.Busy Or IE.ReadyState <> 4
WScript.Sleep 500
Loop
等待页面完全加载。
编辑-虽然这在IE10中有效,但IE8无法定位图像。未在其他版本中进行测试。
在这种情况下,请尝试将您的url更改为
http://www.roku.com/channels?_escaped_fragment_=details/12/netflix#!details/12/netflix
以避免动态生成内容的问题。
此外,在IE8中,需要更改代码以获得图像的类名。应该是
Do While IE.Busy Or IE.ReadyState <> 4
WScript.Sleep 100
Loop
Set imgTags = IE.document.getElementsByTagName("IMG")
For Each imgTag In imgTags
imgClass = imgtag.getAttribute("class")
If IsNull( imgClass ) Then
imgClass = imgTag.className
End If
If imgclass = "cs-poster-big" Then
MsgBox "src is " & imgTag.src
End If
Next
但这不是一个解决方案,只是一个变通办法。