Friday, August 13, 2010

Get the content of a web page and write to file.

There’s been a couple times I wanted to keep copies of web sites to parse the data at a later date. When you don’t want to interact with the page and just need to a simple response I use the request/response methods.  This is one of the few times I don’t use html DOM automation.


This script creates a Microsoft.XmlHttp object for communicating with the web server, it does a simple get request on the Url. The same code could be used to pull down any type of content (jpg, mp3, etc..) given you have the Url.


URL = "http://www.google.com"
' Create the HTTP object
Set xmlhttp = CreateObject("Microsoft.XmlHttp")


' Open connection
xmlhttp.open "GET",  URL , false


' Send the request synchronously
xmlhttp.send ""


' Write the response to a file
Set objFileSys = CreateObject("Scripting.FileSystemObject")
Set file= objFileSys.OpenTextFile("Google.htm", 8, True, 0)
file.Write(xmlhttp.responseText)
File.Close
Post a Comment