Searching objHTTP.ResponseText

I'm trying to make a vb script that retrieves some  text from a web site. Here's what I have so far:

On Error Resume Next

strURL="http://www.saj.usace.army.mil/cco/LakeOWatch/index.shtml"

Set objHTTP = CreateObject("MSXML2.XMLHTTP")
objHTTP.Open "GET", strURL, FALSE
objHTTP.Send

Wscript.echo  objHTTP.ResponseText

The web page I'm accessing is the Lake Okeechobee Watch site which is run by the U.S. Army Corps of Engineers. It reports how deep the lake is each day.

When you run the above script, it retrieves the web site's front page and displays it. So far so good, but I want to be able to display the lake's water level, which is at the end of line 113 in the page source.

Is there a way to search the contents of "objHTTP.ResponseText" for text at a specific location?
mjbtecAsked:
Who is Participating?
 
zoofanConnect With a Mentor Commented:
Modified it once more added the parse to look for "Okeechobee Lake Elevations" as that is the title I think we want.

This will return

-NR-  Difference from 29JUL07

'==Start copy==
Option Explicit      
      Const ForReading = 1, ForWriting = 2, ForAppending = 3 'FileObject Access Type
      Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0 'FileObject Format Type
      Dim sCurPath 'Current Path of Script
      Dim strTempFile 'Name of temp file created and deleted by script
      Dim objWshShell 'Create object for Scripting Shell
      Dim objFso ' Create file system Object for WScript
      Dim strLakepge 'The page we want from the web
      Dim objLakeFile 'Object for reading
      Dim objReadLakeFile 'Object for reading
      Dim intLneCount '
      Dim strFilesaved 'The page returned from the GET
      Dim strLakeLevel ' The text were searchng for
      Dim strTheLevel 'The Our lake level
'Set up Common Objects
      Set objFso = CreateObject("Scripting.FileSystemObject")
      Set objWshShell = WScript.CreateObject("WScript.Shell")
'Set Variables
      sCurPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
      strTempFile = sCurPath & "\lake.csv"
      strLakepge = "http://www.saj.usace.army.mil/h2o/reports/r-oke.txt"
      strLakeLevel = "Okeechobee Lake Elevations"
'Test to see if our temp file exists if so delete it
      If objFso.FileExists(strTempFile) Then
            objFso.deleteFile(strTempFile)
      End If
'Get the file from the site
Call GetPage(strLakePge, strTempFile)
'Read the file into Array
            Set objLakeFile = objFso.GetFile(strTempFile)
            Set objReadLakeFile = objLakeFile.OpenAsTextStream(ForReading, TristateUseDefault)
                  Do Until objReadLakeFile.AtEndOfStream
                        ReDim Preserve arrRegValues(intLneCount)
                        arrRegValues(intLneCount) = objReadLakeFile.ReadLine
                        If InStr(1,arrRegValues(intLneCount),strLakeLevel,1) Then
                        WScript.Echo Mid(arrRegValues(intLneCount),51,Len(arrRegValues(intLneCount))-50)
                        End If
                        intLneCount = intLneCount + 1
                  Loop
            objReadLakeFile.Close
            
Function  GetPage(address, strFilesaved)
Dim DataBin
Dim HTTPGET
Set HTTPGET = CreateObject("Microsoft.XMLHTTP")
HTTPGET.Open "GET", address, False
HTTPGET.Send
DataBin = HTTPGET.ResponseBody
Const adTypeBinary=1
Const adSaveCreateOverWrite=2
Dim SendBinary
Set SendBinary = CreateObject("ADODB.Stream")
SendBinary.Type = adTypeBinary
SendBinary.Open
SendBinary.Write DataBin
SendBinary.SaveToFile strFilesaved, adSaveCreateOverWrite
End Function
Wscript.Quit
'==end copy==


zf
0
 
zoofanCommented:
If your sure it never moves save the response as a temp txt file and read it line by line.

 I did not see the level in the page link you posted, Line 113 in my source was blank.
I did see this page, http://www.saj.usace.army.mil/h2o/reports/r-oke.html 

and found that it is a formatted text file on the server probabaly scripted to html server side  go here instead.

http://www.saj.usace.army.mil/h2o/reports/r-oke.txt

raw text file to read into the script.

zf

0
 
RobSampsonCommented:
Hi, I don't know about the source code viewing, but try this, which looks athe documents InnerText value.
'==============
Set IE = CreateObject("InternetExplorer.Application")

With IE
        .left=200
        .top=200
        .height=400
        .width=400
        .menubar=0
        .toolbar=1  
        .statusBar=0
        .navigate "http://www.saj.usace.army.mil/cco/LakeOWatch/index.shtml"
        .visible=1
End With

'wait a while until IE as finished to load
Do while IE.busy
Loop

strPageText = IE.Document.body.innertext
arrText = Split(strPageText, VbCrLf)
boolStart = False
strLakeLevel = ""
For intCount = LBound(arrText) To UBound(arrText)
      If Trim(arrText(intCount)) = "Everglades Restoration" Then boolStart = True
      If boolStart = True Then
            'MsgBox arrText(intCount)
            If Trim(arrText(intCount)) <> "" And Trim(arrText(intCount)) <> "Everglades Restoration" Then strLakeLevel = arrText(intCount)
      End If
      If strLakeLevel <> "" Then Exit For
Next

MsgBox strLakeLevel
'===============

Regards,

Rob.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
zoofanCommented:
Hi Rob,

This is a good start am still working on.  This goes to the site, and returns the lake statitcs page into a cvs(tab delimitted) file in the same directory as the script.  and will display the line count on completition.

From here we need only to parse the results and grab only the line we want.

So...  go to this page and tell me what data you want to be returned.
http://www.saj.usace.army.mil/h2o/reports/r-oke.txt


'==Start copy==
Option Explicit      
      Const ForReading = 1, ForWriting = 2, ForAppending = 3 'FileObject Access Type
      Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0 'FileObject Format Type
      Dim sCurPath 'Current Path of Script
      Dim strTempFile 'Name of temp file created and deleted by script
      Dim objWshShell 'Create object for Scripting Shell
      Dim objFso ' Create file system Object for WScript
      Dim strLakepge 'The page we want from the web
      Dim objLakeFile 'Object for reading
      Dim objReadLakeFile 'Object for reading
      Dim intLneCount '
      Dim strFilesaved 'The page returned from the GET
'Set up Common Objects
      Set objFso = CreateObject("Scripting.FileSystemObject")
      Set objWshShell = WScript.CreateObject("WScript.Shell")
'Set Variables
      sCurPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
      strTempFile = sCurPath & "\lake.cvs"
      strLakepge = "http://www.saj.usace.army.mil/h2o/reports/r-oke.txt"
'Test to see if our temp file exists if so delete it
      If objFso.FileExists(strTempFile) Then
            objFso.deleteFile(strTempFile)
      End If
'Get the file from the site
Call GetPage(strLakePge, strTempFile)
'Read the file into Array
            Set objLakeFile = objFso.GetFile(strTempFile)
            Set objReadLakeFile = objLakeFile.OpenAsTextStream(ForReading, TristateUseDefault)
                  Do Until objReadLakeFile.AtEndOfStream
                        ReDim Preserve arrRegValues(intLneCount)
                        arrRegValues(intLneCount) = objReadLakeFile.ReadLine
                        intLneCount = intLneCount + 1
                  Loop
                  WScript.Echo intLneCount
            objReadLakeFile.Close
            
Function  GetPage(address, strFilesaved)
Dim DataBin
Dim HTTPGET
Set HTTPGET = CreateObject("Microsoft.XMLHTTP")
HTTPGET.Open "GET", address, False
HTTPGET.Send
DataBin = HTTPGET.ResponseBody
Const adTypeBinary=1
Const adSaveCreateOverWrite=2
Dim SendBinary
Set SendBinary = CreateObject("ADODB.Stream")
SendBinary.Type = adTypeBinary
SendBinary.Open
SendBinary.Write DataBin
SendBinary.SaveToFile strFilesaved, adSaveCreateOverWrite
End Function
Wscript.Quit      
'==END COPY==


zf
0
 
zoofanCommented:
If you would like help finishing the script to parse your data Id be happy to do it just let me know what data based on that text file.

Or if you would like to try it on your own thats great too, Give a shout if you get stuck.

zf
0
 
zoofanCommented:
Sorry found a typo

strTempFile = sCurPath & "\lake.cvs"

Should be

strTempFile = sCurPath & "\lake.csv"

zf
0
 
mjbtecAuthor Commented:
Zoofan's script worked the best. Thanks to everyone for their help. You're all awesome!
0
 
mjbtecAuthor Commented:
So I guess the conclusion here is that there's really no way to search the contents of "objHTTP.ResponseText" for a specific text string.
0
 
RobSampsonCommented:
Hi, I just tested objHTTP.ResponseText, and it can be used in the same way that I parsed the InnerText.
Use this:
'================
strURL="http://www.saj.usace.army.mil/cco/LakeOWatch/index.shtml"

Set objHTTP = CreateObject("MSXML2.XMLHTTP")
objHTTP.Open "GET", strURL, FALSE
objHTTP.Send

strPageText = objHTTP.responseText
arrText = Split(strPageText, VbCrLf)
For intCount = LBound(arrText) To UBound(arrText)
      If InStr(arrText(intCount), "InstanceBeginEditable name=""LakeLevel""") > 0 Then _
            strLakeLevel = Mid(arrText(intCount), InStr(arrText(intCount), "-->") + 3, InStr(Mid(arrText(intCount), InStr(arrText(intCount), "-->") + 3), Chr(10)) - 1)
Next

Set objHTTP = Nothing

MsgBox strLakeLevel
'=============

Regards,

Rob.
0
 
mjbtecAuthor Commented:
Rob, that script works well for me too. Thanks for your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.