如何使用vbscript剥离元素并在消息框中显示?

时间:2012-05-22 03:59:35

标签: vbscript

我想找到2年合约的价格并将其显示在消息框中。 Sofar我有:

Dim MyPage
Dim Price
Set MyPage=CreateObject("Microsoft.XMLDOM")
MyPage.load("http://www.verizonwireless.com/b2c/store/controller?item=phoneFirst&action=viewPhoneDetail&selectedPhoneId=5723")
Wscript.Sleep 2000
Set Price = MyPage.getElementsByTagName("span")
For Each Elem In Price
   MsgBox(Elem.firstChild.nodeValue) 
Next

我知道我完全错了,但我甚至不知道从哪里开始。我喜欢编写这样的简单程序,但我需要帮助才能开始。任何想法都会有所帮助!

3 个答案:

答案 0 :(得分:2)

这是一个更好的版本,使用HTMLFile对象

Dim HTMLDoc, XML, URL, table
Set HTMLDoc = CreateObject("HTMLFile")
Set XML = CreateObject("MSXML2.XMLHTTP")
URL = "http://www.verizonwireless.com/b2c/store/controller?item=phoneFirst&action=viewPhoneDetail&selectedPhoneId=5723"

With XML
  .Open "GET", URL, False
  .Send
  HTMLDoc.Write .responseText
End With

Set spans = HTMLDoc.getElementsByTagName("span")
for each span in spans
  WScript.Echo span.innerHTML
next

'=><SPAN>Set Location</SPAN>
'=>Set Location
'=><SPAN>Submit</SPAN>
'=>Submit
'=>Connect with us

答案 1 :(得分:1)

你使用的控件是读取XML文档,你需要这样的东西

'Create an xmlhttp object, the string depends on the version that is installed 
'on your pc could eg also be "Msxml2.ServerXMLHTTP.5.0"
Set xmlhttp = CreateObject("Microsoft.XMLHTTP")
xmlhttp.Open "GET", "http://admin:pasword@10.0.0.2/doc/ppp.htm", False 
xmlhttp.Send
text=xmlhttp.responseText
wscript.echo text
Set xmlhttp = Nothing

在注册表中运行XMLHTTP搜索,以获取标识符的正确字符串/版本。

要从html获取标记,您可以使用以下

text = "blabla <span>this is what i need</span> bla bla<span>second item</span> end"

function getElementsByTagName(sTextToSeachIn, tag)
  answer = ""
  separator = ""
  set oRegExpre = new RegExp
  with oRegExpre
    .IgnoreCase = true
    .Global = true
    .MultiLine = True
    .Pattern = "<" & tag & ">(.*?)</" & tag & ">"
  end with
  set oColMatches = oRegExpre.Execute(sTextToSeachIn)
  for each match in oColMatches
    answer = answer & separator & match.subMatches(0)
    separator = "|" 'use something that's not in the spancontents
  next
  if separator <> "" then
    getElementsByTagName = split(answer, separator)
  else
    getElementsByTagName = array()
  end if
end function

for each tag in getElementsByTagName(text, "span")
  wscript.echo tag
next

'=>this is what i need
'=>second item

有更好的技术,当然还有比vbscript更好的语言来做这件事,我建议看一下这些东西中的Ruby。

答案 2 :(得分:-1)

Alex回应你关于获取cookie并在HTMLFile中运行javascript的评论,这里发现了一个ruby脚本,希望它在某些时候帮助你,它在页面中读取,将它传递给HTLMFile对象并在DOM执行远程javascript文件。它还让您了解activeX和Ruby的综合功能。

require "win32ole"

$jsxpath_uri = "http://svn.coderepos.org/share/lang/javascript/javascript-xpath/trunk/release/javascript-xpath-latest-cmp.js"
uri, xpath = "http://gist.github.com/gists", "//div[@class='info']/span/a"

http = WIN32OLE.new('MSXML2.XMLHTTP')
http.Open "GET", uri, false
http.Send
text = http.responseText

dom = WIN32OLE.new("htmlfile")
dom.Write(text)
dom.parentWindow.eval(open($jsxpath_uri){|f| f.read })

items = dom.evaluate(xpath, dom, nil, 7, nil)
len = items.snapshotLength
(0...len).each do |i|
  item = items.snapshotItem(i)
  puts item.innerHTML
end