1

我想找到 2 年合同的价格并将其显示在消息框中。到目前为止,我有:

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

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

4

3 回答 3

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
于 2012-05-23T13:34:05.513 回答
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,它在这些事情上表现出色。

于 2012-05-22T11:11:23.777 回答
-1

亚历克斯,作为对您关于获取 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
于 2012-05-24T08:11:16.140 回答