How can I strip the element using vbscript and dis

2019-08-02 05:51发布

I would like to find the price with 2 year contract and display it in a message box. Sofar I have:

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

I understand that I am completely wrong, but I don't even know where to start. I love writing simple programs like this, but I just need help getting started. Any ideas will help!

标签: vbscript
3条回答
We Are One
2楼-- · 2019-08-02 06:39

the control you use is for reading XML documents, you need something like this

'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

Run a search in your registry for XMLHTTP to get the right string/version for the identifier.

To get the tag from the html you can use the following

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

There are better techniques and certainly better languages than vbscript to do this, i suggest to take a look at Ruby which exels in such things.

查看更多
姐就是有狂的资本
3楼-- · 2019-08-02 06:57

Alex, in response to your comment about getting a cookie and running a javascript in HTMLFile, here a ruby script i found, hopes it helps you at some point, it reads in a page, passes it to the HTLMFile object and in that DOM executes a remote javascript file. It also gives you an idea of the combined power of activeX and 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
查看更多
别忘想泡老子
4楼-- · 2019-08-02 06:58

Here a better version, uses the HTMLFile object

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
查看更多
登录 后发表回答