English 中文(简体)
如何使用 vbscript 将元素脱光, 并在信息框中显示?
原标题:How can I strip the element using vbscript and display in message box?
  • 时间:2012-05-22 03:59:35
  •  标签:
  • vbscript

我想找到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

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

最佳回答

在此更好的版本, 使用 HTML 文件对象

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
问题回答

您使用的控件是用于读取 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:[email protected]/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

我建议大家看看Ruby, 鲁比在这种事情中出声。

亚历克斯,为了回应你的评论 关于在 HTMLFile 中买到一个饼干 和运行一个笔记本, 在这里,一个红宝石脚本我找到了, 希望它能帮助你 在某个时刻, 它读到一页, 把它传递到 HTLMFile 对象, 在 DOM 执行一个远程的 笔记本文件。 它也让你了解了活动X 和 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




相关问题
What approach should I use to test a VBScript?

I ve been asked to help out with a project which has made extensive use of VBScript to process a whole bunch of text files and generate certain outputs - sanitized files, SQL entries etc.. The script ...

Unable to call c# code from vbscript - ActiveX error

I am trying to call a method I have written in C# from VBScript. I have followed just about all of the instructions I can find on the web and am still having problems. Specifically I am getting Error:...

How to set delay in vbscript

How to set delay in vbscript? WScript.Sleep(100) does not work on Windows XP, Vista.

Using Classes in a Dictionary in Classic ASP

I usually do C# but have inherited a classic ASP project. I have defined a class: Class clsPayment Public Name End Class Set objPayment = New clsPayment objPayment.Name = "...

How to check which Operating System?

How can I check OS version in a batch file or through a vbs in an Windows 2k/2k3 environment ? You know ... Something like ... : "If winver Win2k then ... or if winver Win2k3 then ....

Problem casting field when querying spreadsheet

I am trying to query an .xls spreadsheet with VBScript, but I have run into an issue when trying to cast a field. I connect to the spreadsheet like this. Set objConnection = CreateObject("ADODB....

热门标签