1. How to scrape a website.
2. How to use special software to make writing Regular Expressions easier.
3. How to write Regular Expressions in C#.
This tutorial is intended for people who know how to do some basic Regular Expressions and know the syntax of the language.
Since there is a lot of material to cover I won’t go over everything. If you want to see the program in action or the bits I left out download the fully commented source code.
Till a little while ago I never really thought much of Regular expressions. I used them a couple of times just to validate an email address. It was not till one day I was approached to make a web scraping program that would deal with pages from amazon.com and then store all the grabbed information in an xml file.
At first I did not even know how to do this. The first thing I had to tackle was getting the ASIN number (A unique number Amazon uses for its products). The only solution I could think of was read each line of code looking for the product links and then using a string builder to break up that link and using a for-loop putting the ASIN back together once found.
I got to this work but quickly realized that doing something similar will not work for all the fields I needed to get. On top of that the program would probably be slow seeing that I would have to check almost 2200 lines of code one line at a time.
So I thought to myself there must be a better way to do this. Later on someone suggested why not use Regular expressions. My initial thought was I am not going to be validating an email address here I am trying to grab content of this amazon.com page.
It was not till I picked up this gem of a book called Mastering Regular Expressions by Jeffrey Friedl that I finally realized the power of Regular Expressions.
How to scrape a website
The first thing we have to do is make the scraper program. A scraper is a program that basically just downloads all the source code of the webpage.
1. Fire up Visual Studios
2. File –> New Website -> Asp.net Website -> Ok
3. Make a new .aspx file(right click on your solution and click on “add a new item”)
4. Name this page “Scraper.aspx”
5. Go to the code behind file(Scrapper.aspx.cs)
Now it’s time to start writing the C# to download the page.
The first thing you will have to do is add to packages to your Using statements.
using System.Net;
using System.IO;
For the shake of this tutorial we will just write all the code in the Page_Load method.
The comments should explain what each line is doing. The big picture though is on page load where going to download all the source code of the given page that has been hardcoded into the HttpWebRequest.Create() method.
Now we have this giant string with all the source code. Now before we go diving in on how to extract the information we want with Regular Expressions we will first learn how to use a program called Espresso that will make our life so much easier.
Expresso
When I started the scraper program for the client I quickly found that it was very hard to write long regular expressions. Every time I wrote something it always came back with no results. I thought to myself this will take forever till I make a Regular expression that works. I then set out to find if someone had a program that would make it easier to write regular expressions. After much searching I found a program called Expresso, a free program that makes writing Regular expressions easier.
First grab a copy of Expresso from their page
So we are going to make a scraping program that will grab the Title of this book, book authors, list price, price and how much you can save.
Regular expressions are just basically patterns so in order to grab information from this area we will have to make some kind of pattern to grab the information. So we have to learn how they displayed this information to us.
If you would look at the source code you would see that their code for this page is about 14,828 lines of code(this include blank lines). That’s a lot of code to go through to find the information that we need.
If you’re using in firefox I highly recommend you grab firebug. With firebug you will be able to hover over the area you want and it will show you the source code of that area.
With using firebugs inspect feature then hovering over all the parts of I wanted (everything in the above picture). I found that this is the code where interested in. With using the inspect feature it took me only a couple minutes to find the 30 lines of code where interested it in.