如何使用vbscript从远程网页获取元素值?
伙计们, 我是noob,我想编写一个本地vbscript,从远程网页获取一些值。 网页上有一些片段如何使用vbscript从远程网页获取元素值?,vbscript,Vbscript,伙计们, 我是noob,我想编写一个本地vbscript,从远程网页获取一些值。 网页上有一些片段 <div id="profile_content" class="dxb_bc"> <div> <div class="hm"> <p> <a href="space-uid-52433.html" target="_blank">
<div id="profile_content" class="dxb_bc">
<div>
<div class="hm">
<p>
<a href="space-uid-52433.html" target="_blank">
<img src="http://bbs.dealmoon.com/uc_server/avatar.php?uid=52433&size=middle" />
</a>
</p>
<h2 class="mbn">
<a href="space-uid-52433.html" target="_blank">LittleCar</a>
</h2>
</div>
<ul class="xl xl2 cl ul_list">
<li class='ul_ignore'>
<a href="home.php?mod=spacecp&ac=friend&op=ignore&uid=52433&handlekey=ignorefriendhk_52433" id="a_ignore_52433" onclick="showWindow(this.id, this.href, 'get', 0);">AAAAAA</a>
</li>
<li class='ul_msg'>
<a href="home.php?mod=space&uid=52433&do=wall">BBBBBB</a>
</li>
<li class='ul_poke'>
<a href="home.php?mod=spacecp&ac=poke&op=send&uid=52433&handlekey=propokehk_52433" id="a_poke_52433" onclick="showWindow(this.id, this.href, 'get', 0);">CCCCCC</a>
</li>
<li class='ul_pm'>
<a href="home.php?mod=spacecp&ac=pm&op=showmsg&handlekey=showmsg_52433&touid=52433&pmid=0&daterange=2" id="a_sendpm_52433" onclick="showWindow('showMsgBox', this.href, 'get', 0)">DDDDDD</a>
</li>
</ul>
</div>
</div>
</div>
但我得到了一个错误,类似于不支持的方法。我所能做的只是在vbscript中按id获取元素。我还没有找到任何有助于解决我问题的东西。所以我在这里
我已经问了一个类似的问题,这个问题被搁置了。我的名声因此降低了两倍。我不敢相信没有人能帮助我。
谢谢使用
' Note the "s" in getElementsByTagName
Set collATags = IEApp.Document.getElementsByTagName("a")
' ^ there it is
现在,您可以像这样迭代它们:
For Each aTag in collATags
Wscript.Echo aTag.outerHtml
Next
编辑:
要获取AAAAA之类的特定文本,请使用innerHtml属性:
For Each aTag in collATags
If aTag.innerHtml = "AAAAA" then
' Found it!
Set foundTag = aTag
Exit for
End if
Next
要从ID标记缩小到特定标记,可以使用以下方法:
Set profileContentElement = document.GetElementById("profile-content")
使用此元素可获取具有标记名的所有元素:
Set collATags = profileContentElement.getElementsByTagName("a")
并使用上述方法对元素进行迭代,以获得AAAAA文本为innerHtml的元素
EDIT2:
要获取标识符不是id
的元素,请获取id正确的父元素,在标记名上获取childcollection,并在outerHtml上过滤正确的元素:
' Get the correct parent
Set profileContentElement = document.GetElementById("profile-content")
' Get the childcollection with the A tag
Set collATags = profileContentElement.getElementsByTagName("a")
' Iterate through the collection
Set foundTag = Nothing
For Each aTag in collATags
If aTag.outerHtml = "home.php?mod=space&uid=52433&do=wall" then
' Found it!
Set foundTag = aTag
Exit for
End if
Next
' Get the text in the foundTag
If not foundTag Is Nothing Then
wscript.echo "Woei, found the linktext, it is: " & foundTag.innerHtml
End If
注意:此处没有Windows计算机,这是未经测试的代码。Thx。我理解迭代的用法。但是如果我只想得到“aaaaa”,我该怎么做呢?如果我使用特定标记的迭代,那么获取值将花费很长时间。是否有任何方法可以快速定位标记并获取其值?如果我想得到我不知道的值,我该怎么办?e、 例如,我想回显值“BBBBBB”,我可以通过一些方法获得该值吗,比如getElementByXXX.innerHTML?@franksunn;你对那个链接了解多少?它总是第四个链接->获取第四个元素。它是否总是包含字符串
&do=wall
->获取A
标记的outerHtml,查看它是否包含该字符串并获取该链接的innerHtml。你需要一个唯一的标识符来定义你想要的链接,你不能说“给我一些我知道是正确的链接,但是脚本必须猜测”。如果我想使用“home.php?mod=space&uid=52433&do=wall”作为唯一标识符,我应该怎么做?:)
Set collATags = profileContentElement.getElementsByTagName("a")
' Get the correct parent
Set profileContentElement = document.GetElementById("profile-content")
' Get the childcollection with the A tag
Set collATags = profileContentElement.getElementsByTagName("a")
' Iterate through the collection
Set foundTag = Nothing
For Each aTag in collATags
If aTag.outerHtml = "home.php?mod=space&uid=52433&do=wall" then
' Found it!
Set foundTag = aTag
Exit for
End if
Next
' Get the text in the foundTag
If not foundTag Is Nothing Then
wscript.echo "Woei, found the linktext, it is: " & foundTag.innerHtml
End If