Friday, February 5, 2010

Screen Scrapes in VB.NET

Often you have to do a screen scrape of a web page and get the HTML source. In this little segment you can take a URL and get the Source in a text box.

Sub ScrapeData(ByVal url)
Dim req As HttpWebRequest = WebRequest.Create(url)
req.Timeout = 5000
Try
Dim resp As HttpWebResponse = req.GetResponse()
Catch wex As WebException
If wex.Status = WebExceptionStatus.Timeout Then
MsgBox("Data Web Request Timed out", MsgBoxStyle.Exclamation, AppTitle)
Else
MsgBox("There was some exception: " & wex.Message, MsgBoxStyle.Critical,
AppTitle)
End If
End Try
Try
Dim resp As HttpWebResponse = req.GetResponse()
Dim sr As New StreamReader(resp.GetResponseStream())
Dim results As String = sr.ReadToEnd()
G:\Programming\VBProjects\ScrapeMailerApp\frmScape.vb 2
sr.Close()
TextBox1.Text = results
Catch wex As WebException
MsgBox("There was some exception: " & wex.Message, MsgBoxStyle.Critical)
End Try
End Sub
Private

No comments:

Post a Comment