2014-01-20 72 views
1

伙计们, 我是noob,我想写一个本地vbscript从远程网页获取一些值。 有一些网页片段。如何使用vbscript从远程网页获取元素的值?

<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> 

我的问题很容易。我只想得到'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。我无法相信没有人能帮助我。 感谢

回答

1

获得一个标签与

' 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机器在这里,这是未经测试的代码。

+0

Thx。我明白使用迭代。但如果我只想获得'AAAAAA',我该怎么办? – franksunnn

+0

如果我使用特定标签的迭代,则需要很长时间才能获得该值。有没有方法可以快速找到标签并获得其价值? Thx – franksunnn

+0

如果我想获得我不知道的价值,我该怎么做?例如,BBBBBB我想回显'BBBBBB'的值,我可以通过getElementByXXX.innerHTML等方法获取值吗? – franksunnn

相关问题