Free Web Hosting by Netfirms
Web Hosting by Netfirms | Free Domain Names by Netfirms

 

Webscrape

A Web 'Screen Scraper' 

mailto:info@webscrape.com


Calling PageScrape from VBScript using the Windows Script Host

 

In this example we create a little VB Script to get a brief synopsis of the weather in Ireland from http://www.met.ie and then display it in a windows dialog.  The output will look something like that shown below (spelling mistakes and all?). 

 

 

To get the weather synopsis we will invoke PageScrape as follows:

pscrape -u"www.met.ie" -e"Brief.*font.*>(.*)</font"

All we need to do is to call the above command from VBScript (VBS) and then display the output.  To launch another program from VBS we can use the WshShell.Run() method as follows:

' Create the objects we need
Set shell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")


' Generate Temp Filename which PageScrape will output to

fileName = fso.GetTempName

' Build command, in a VBS string literal, " must be escaped as ""
' The -o option tells PageScrape to write the matched text to a file
run = "pscrape -u""www.met.ie"" -e""Brief.*font.*>(.*)</font"" -o""" & fileName & """"


' Run PageScrape synchronously in a hidden window
' The 0 specifies that the Window should be hidden
' The 1 specifies that we should wait for the command to complete
' before continuing

shell.Run run, 0, 1


'
Open the Temp File and read its contents
result = fso.OpenTextFile(fileName, 1).ReadAll

' Delete the Temp file
fso.DeleteFile filename

' Now output the Bad News..
WScript.echo result


We could have used the newer Exec() method and read PageScrape's output directly from the provided Stdout object but Exec() gives us no control over the window in which PageScrape runs, an ugly console window flashes up.  We don't have this problem with Run() as we can request that the window be hidden,  but we need to cheat to get at PageScrape's output as Run() does not provide access to Stdout.   We use PageScrape's -o option which gets it to write to a temporary file rather than Stdout.  We then read the contents of this file from the script.