Screen scraping in C#

Feb 13, 2007

Some say that screen scraping is a lost art because it is no longer an advanced discipline. That may be right, but there are different ways of doing it. Here are some different ways that all are perfectly acceptable, but can be used for various different purposes.

Old school

It’s old school because this approach has existed since .NET 1.0. It is highly flexible and lets you make the request asynchronously.

public static string ScreenScrape(string url)
{
 System.Net.WebRequest request = System.Net.WebRequest.Create(url);
 // set properties of the request
 using (System.Net.WebResponse response = request.GetResponse())
 {
  using (System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream()))
  {
   return reader.ReadToEnd();
  }
 }
}

Modern>

In .NET 2.0 we can use the WebClient class, which is a cleaner way of solving the same problem. It is equally as flexible and can also work asynchronous.

public static string ScreenScrape(string url)
{
 using (System.Net.WebClient client = new System.Net.WebClient())
 {
  // set properties of the client
  return client.DownloadString(url);
 }
}

The one-liner>

This is a short version of the Modern approach, but it deserves to be on the list because it is a one-liner. Tell a nineteen ninetees developer that you can do screen scraping in one line of code and he wont believe you. The approach is not flexible in any way and cannot be used asynchronously.

public static string ScreenScrape(string url)
{
 return new System.Net.WebClient().DownloadString(url);
}

That concludes the medley of screen scraping approaches. Pick the one you find best for the given situation.

* Only $4.95/month ASP.NET & Windows 2008 + IIS 7 Hosting! FREE SQL Included

Comments (11) -

 Eber Irigoyen
Eber Irigoyen
2/13/2007 6:47:23 PM #

you mean Web scraping...

Mads Kristensen
Mads Kristensen
2/13/2007 6:51:25 PM #

Yes I do Smile

 Marco
Marco
2/15/2007 5:23:47 AM #

I suppose that it works well with the text of web pages. But if I need to download also the images, probably I will need many more lines Smile

 Claus
Claus
2/17/2007 7:10:43 PM #

Old school (?!?)

WebClient is just a specific implementation of HTTPWebRequest/Response. It is a "wrapper" class.

The WebClient class uses the WebRequest class to provide access to resources. WebClient instances can access data with any WebRequest descendant registered with the WebRequest.RegisterPrefix method.

Mads Kristensen
Mads Kristensen
2/17/2007 8:25:14 PM #

Claus, it's old school compared to WebClient because it the WebClient didn't exist before .NET 2.0.

 Claus
Claus
2/18/2007 6:26:38 AM #

Not true Mads,

.NET Framework
Supported in: 2.0, 1.1, 1.0 <----  msdn2.microsoft.com/.../...t.webclient(VS.80).aspx

 Bill Mill
Bill Mill
2/22/2007 2:15:17 PM #

Tell a 1990s python, perl, or lisp programmer that he can screen-scrape in one line, and he'll say "of course I can".

What happens when you tell a 2007 C# coder that he *just* got the ability to screen scrape in less than 5 lines of code? Apparently he gets excited.

(Note: I am a professional C# coder, not just a sniper. I was, however, pissed off just the other day when I had to write my own 10-line function to screen scrape a web page. Now if opening a file and reading it could just happen in less than 5 lines of code...)

bikini girls
bikini girls United States
5/26/2008 3:13:12 AM #

Is there something better for windows application .net 2.0 web scraping like some kind of framework, or is WebClient the best to use?

web directory
web directory Canada
5/26/2008 3:20:53 AM #

I'm pretty sure there is some kind of Spider Class either as part of c# or perhaps it was in the .net FCL, read it on a forum somewhere.  If I find the link i'll post it back.

shri mohan
shri mohan India
7/26/2008 12:52:09 PM #

Hi all..

I want to develop a RSS feed for a website in c# using screen scrapping..can somone help me asap..

thx a lot in advance

reliable web hosting
reliable web hosting Canada
8/7/2008 4:03:57 AM #

In regards to that spider class. I found one also one time called the Snoopy web scraper class.  However, I cannot find it anymore.  I think the .net 1.0 method is better as it seems more flexiable.  Is there anything you can't do with the .net 2.0 method that you can do with the .net 1.0 method?  You say asynchronous, what exactly does that imply?

Jeremy,

Pingbacks and trackbacks (1)+

Comments are closed

About the author

Mads Kristensen

Mads Kristensen
Program Manager at the Microsoft Web Platform team and founder of BlogEngine.NET.

More...

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.