The text from a website can be scraped in many ways. Now, I will show two simple ways to scrape text. They are:
- WebClient class
- WebRequest / WebResponse class
I have used both classes in the sample project.
I have scraped the following highlighted text from a website.
Using the WebClient
I start the code explanation with the WebClient class.
The WebClient class provides common methods for sending data or receiving data from any local, intranet, or Internet resource identified by a URI.
The Webclient methods have various methods to download data from the URL. But, I will use a method call DownloadString.
The method DownloadString allows the download of a string from any local, intranet, or internet resource identified by a URI and return the string.
Check the following code:
- WebClient wb= new WebClient();
- String searchquery=textBox1.Text;
- String scrapdata;
- scrapdata=wb.DownloadString(searchquery);
A Wb object has been created for the Webclient class and the wb object uses the Downloadstring methods to download the string from the URI, then return the string that has been assigned to scrape the data. The code downloads only the source code of the website. We have split the specific text from the source code using the Regex.
Regexes
A regular expression is a pattern that can be matched against an input text.
Before using the Regex, include the following namespace in the C# code.
- using System.Text.RegularExpressions;
Regex.Matches returns multiple Match objects. It matches multiple instances of a pattern and returns a MatchCollection. It is advantageous for extracting values, based on a pattern, if many values are expected.
Regex.Matches extracts text between specific tags. See the code below:
- MatchCollection data=Regex.Matches(scrapdata,@"<title>\s*(.+?)\s*</title>",RegexOptions.Singleline);
The RegexOptions.Singleline option, or the s inline option, causes the regular expression engine to treat the input string as if it consists of a single line. It does this by changing the behavior of the period (.) language element so that it matches every character, instead of matching every character except for the newline character\n or \u000A.
Then, I have used a foreach loop to find the exact value.
See the code below:
- foreach (Match m in data)
- {
- String downtitle = m.Groups[1].Value;
- MessageBox.Show(downtitle.ToString());
- }

So, I have extracted the second matching value using the foreach loop.
Using the WebRequest / WebResponse Class
The WebRequest is an abstract base class. So we actually don't use it directly. We can use it using its derived classes.
We need to use the Create method of WebRequest to create an instance of WebRequest. GetResponseStream returns a data stream. The following is the code:
- WebRequest request = WebRequest.Create (searchquery);// Create a request for the URL.
- request.Credentials = CredentialCache.DefaultCredentials;// If required by the server,
- // set the credentials.
- WebResponse response = request.GetResponse ();// Get the response.
- Stream dataStream = response.GetResponseStream (); // Get the stream containing content
- // returned by the server.
- StreamReader reader = new StreamReader (dataStream); // Open the stream using a StreamReader
- string responseFromServer = reader.ReadToEnd (); // Read the content.
References

Ammar ShaukatPosted Mar 3, 2016, 12:15 PM
Good work .
Arul RPosted Jan 11, 2016, 8:21 AM
Nice share
Santhakumar MunuswamyPosted Jul 28, 2015, 3:07 PM
Good. Thanks for sharing
Sibeesh VenuPosted Jul 28, 2015, 7:52 AM
Nice Share
Pankaj Kumar ChoudharyPosted Jul 28, 2015, 7:38 AM
Nice One Sir..........
Jaipal ReddyPosted Jul 28, 2015, 7:31 AM
good one.
Nilesh JadavPosted Jul 28, 2015, 6:23 AM
Nice one sir
Sam HobbsPosted Jul 28, 2015, 5:28 AM
Regular expressions can work for simple things but they can get complicated for more complex requirements. If a regular expression solution is chosen for a project that seems simple but actually is not then unfortunately many developers will tend to continue with their investment of time using regular expressions.
Debasis SahaPosted Jul 28, 2015, 4:49 AM
Good One..
Gowtham RajamanickamPosted Jul 28, 2015, 4:29 AM
good article..
Gopi ChandPosted Jul 28, 2015, 3:58 AM
Nice effort
Rahul Kumar SaxenaPosted Jul 28, 2015, 3:52 AM
Good Show
Neeraj KumarPosted Jul 28, 2015, 3:48 AM
Nice
Rajeesh MenothPosted Jul 28, 2015, 3:21 AM
Good Article..
RakeshPosted Jul 28, 2015, 3:09 AM
Good