grepsedawk
grepsedawk

Reputation: 3411

How can I strip the element using vbscript and display in message box?

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!

Upvotes: 1

Views: 5826

Answers (3)

peter
peter

Reputation: 42207

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

Upvotes: -1

peter
peter

Reputation: 42207

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

Upvotes: 2

peter
peter

Reputation: 42207

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:[email protected]/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.

Upvotes: 1

Related Questions