Script to grab web page

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • drhowarddrfine
    Recognized Expert Expert
    • Sep 2006
    • 7434

    #16
    Thanks! I'll give it a try later today and let you know.

    Comment

    • Curtis Rutland
      Recognized Expert Specialist
      • Apr 2008
      • 3264

      #17
      One thing I forgot to mention is that script isn't conditional...i t will print the response, no matter what the response was. You might want to implement some if-then-else logic.

      Also, you might want to add this line after line 6:
      Code:
      xmlhttp.waitForResponse()
      If you do decide to do it conditionally, you might be interested in this article, which shows all the members and methods of the MSXML2.ServerXM LHTTP object.

      Comment

      • Curtis Rutland
        Recognized Expert Specialist
        • Apr 2008
        • 3264

        #18
        Here, slightly more refined:
        Code:
        'define vars
        url = "http://bytes.com" 
        path = "C:\dev\vbs\temp.txt"
        'instantiate
        Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP") 
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set txtfile = fso.OpenTextFile(path, 2, True)
        Set oWS = WScript.CreateObject("WScript.Shell")
        'make request
        xmlhttp.open "GET", url, false 
        xmlhttp.send ""
        'wait for response
        xmlhttp.waitForResponse()
        'if status is 200, then it's OK
        if xmlhttp.status = 200 then
            txtfile.WriteLine(xmlhttp.responseText)
        	txtfile.Close
        	'enable this to print
        	'oWS.Run "NotePad.exe /p " + path
        	'enable this to just display
        	oWS.Run "notepad.exe " + path	
        else
        	'popup bad response, or just omit to end
        	WScript.Echo("bad response")
        end if
        'destroy objects.  I'm not sure this is necessary
        Set txtfile = nothing
        Set xmlhttp = nothing
        Set oWS = nothing
        Set fso = nothing
        It's a bit longer because I've included comments.

        Now, the program will check that the HTTP status is 200 before proceeding. Right now it will let you know it failed, but if you remove lines 22-24, you will get rid of the popup.

        If you want it to print, uncomment 19 and comment 21.

        Hope this helps.

        Comment

        • drhowarddrfine
          Recognized Expert Expert
          • Sep 2006
          • 7434

          #19
          Thanks again. I'm floating around right now and that's why I haven't tried it yet.

          Comment

          • drhowarddrfine
            Recognized Expert Expert
            • Sep 2006
            • 7434

            #20
            Success! That's just alright, man. I need to put this on my wife's desktop with the printer to test that part out but Notepad opens the file automatically as is.

            The only thing I need to add would be a timer to make it fetch every minute or two. It would be nice if it could print a rendered html page but that can wait.

            I'll look later but if you know where the docs are for the timer, I can look it up. Also, if you already know what I should look into for rendering the html for the printer, that would be just great. For now, plain text will be good enough.

            Saved me some grief. Thanks again!

            Comment

            • Curtis Rutland
              Recognized Expert Specialist
              • Apr 2008
              • 3264

              #21
              As to the timer, you should just use the windows task scheduler to do it.

              Comment

              Working...