如何使用 vbscript 从远程网页获取元素的值? [英] How can I get values of elements from remote webpages using vbscript?
问题描述
伙计们,我是菜鸟,我想编写一个本地 vbscript 来从远程网页获取一些值.有网页的一些片段.
<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><ul class="xl xl2 cl ul_list">
<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> <a href="home.php?mod=space&uid=52433&do=wall">BBBBBB</a> <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 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
我的问题很简单.我只是想获得价值 'LittleCar' 'AAAAAA' 'BBBBBB' 等等.我尝试编写 vbscript 来捕获元素:
<a href="space-uid-52433.html" target="_blank">LittleCar</a>
像这样:
IEApp.Document.getElementById("profile_content").getElementByTagName("a").Item(1)
但是我遇到了类似不受支持的方法之类的错误.我能做的就是在 vbscript 中通过 id 获取元素.我没有找到任何有价值的东西来解决我的问题.所以我来了.
我已经问过一个类似的问题,但被搁置了.我的声望因此降低了 2.我不敢相信没有人可以帮助我.谢谢
获取 A 标签的集合
' 注意 getElementsByTagName 中的s"设置 collATags = IEApp.Document.getElementsByTagName("a")' ^ 就是这样
现在你可以像这样迭代它们:
对于collATags中的每个aTagWscript.Echo aTag.outerHtml下一个
要获得像 AAAAA 这样的特定文本,请使用 innerHtml 属性:
对于collATags中的每个aTag如果 aTag.innerHtml = "AAAAA" 那么' 找到了!设置 foundTag = aTag退出万一下一个
要从带有 ID 的标签缩小到特定标签,您可以使用:
设置 profileContentElement = document.GetElementById("profile-content")
使用该元素获取所有带有标签名称的元素:
Set collATags = profileContentElement.getElementsByTagName("a")
并使用上面描述的方法遍历元素,得到带有 AAAAA 文本的innerHtml
为了获取标识符不是id
的元素,获取具有正确ID的父元素,获取Tagname上的子集合并过滤outerHtml上的正确元素:
' 获取正确的父级设置 profileContentElement = document.GetElementById("profile-content")' 获取带有 A 标签的子集合设置 collATags = profileContentElement.getElementsByTagName("a")' 遍历集合设置 foundTag = 无对于 collATags 中的每个 aTag如果 aTag.outerHtml = "home.php?mod=space&uid=52433&do=wall" 那么' 找到了!设置 foundTag = aTag退出万一下一个' 获取 foundTag 中的文本如果没有找到Tag Is Nothing Thenwscript.echo "哇,找到链接文本,它是:" &foundTag.innerHtml万一
注意:这里没有 Windows 机器,这是未经测试的代码.
Guys, I'm noob and I wanted to write a local vbscript to get some values from a remote webpage. There are some snippets of the webpage.
<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>
My problem is very easy. I just wanted to get the value 'LittleCar' 'AAAAAA' 'BBBBBB' and so on. I tried to write vbscript to capture the element :
<a href="space-uid-52433.html" target="_blank">LittleCar</a>
like this:
IEApp.Document.getElementById("profile_content").getElementByTagName("a").Item(1)
but I got an error something like unsupported method. What I can do is just to get the element by id in vbscript. I haven't found anything valued for solving my question. So I'm here.
I've already asked a similar question which is put on hold. My reputation has been decreased by 2 for that. I cannot believe there's no one can help me. Thanks
Get the collection of A tags with
' Note the "s" in getElementsByTagName
Set collATags = IEApp.Document.getElementsByTagName("a")
' ^ there it is
Now you can iterate over them like:
For Each aTag in collATags
Wscript.Echo aTag.outerHtml
Next
EDIT:
To get a specific text like AAAAA, use the innerHtml property:
For Each aTag in collATags
If aTag.innerHtml = "AAAAA" then
' Found it!
Set foundTag = aTag
Exit for
End if
Next
To narrow down to a specific tag from an tag with an ID you can use this:
Set profileContentElement = document.GetElementById("profile-content")
Use this element to get all elements with a tag name:
Set collATags = profileContentElement.getElementsByTagName("a")
And use the method described above to iterate through the elements to get the one with the AAAAA text as innerHtml
EDIT2:
In order to get the element with an identifier that is not id
, get the parent element with the correct ID, get the childcollection on Tagname and filter the correct element on 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
note: No Windows machine here, this is untested code.
这篇关于如何使用 vbscript 从远程网页获取元素的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!