Creating A Link And Filter In C#

Introduction

In this blog, we will learn how to extract all the links from a Webpage, using a Web client. Thus, without wasting time, let's dive directly into the code.

Step 1

Thus, we are creating a link grabber. For it, we need some logic and it's always a good idea to clarify the logic before creating something. Thus, let's define the logic.

  1. We need a link for the page to crawl. We can get the link from a TextBox.
  2. Now, we have the link. The next step will be to download the Web page to crawl. We can either use a Web client for it or a WebBrowser control.
  3. Now, we have a HTML document. The next step is to extract the links from that page.
  4. As we know, most of the useful links are contained in href attribute of the anchor tags.
  5. Now, up to that point, we know that we want to grab the anchor elements of the page. Thus, we can do this, using getElementsByTagName().
  6. Now, we have the collection of all the anchor elements.
  7. The next step is to get href attribute and add it to a list. Let this list be a check box list.
  8. Now, we have all the extracted links.

Step 2

Open Visual Studio and choose "New project".

Visual Studio

  1. Now, choose "Visual C#" -> Windows -> "Windows Forms Application".

    Visual Studio

  2. Now, drop a text box from the Toolbar onto the form.

    Visual Studio

  3. Now, drop a button from the Toolbar onto the form and name it "grab".

    Visual Studio

  4. Now, add one check list box from the Toolbar menu onto the form.

    Visual Studio

  5. Now, double-click on the button to generate the click handler.
  6. Add the code, mentioned below, for the click handler.

The following code is

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10. namespace linkGrabber {  
  11.     public partial class Form: Form {  
  12.         public Form() {  
  13.             InitializeComponent();  
  14.         }  
  15.         private void button_Click(object sender, EventArgs e) {  
  16.             WebBrowser wb = new WebBrowser();  
  17.             wb.Url = new Uri(textBox.Text);  
  18.             wb.DocumentCompleted += wb_DocumentCompleted;  
  19.         }  
  20.         void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {  
  21.             HtmlDocument source = ((WebBrowser) sender).Document;  
  22.             extractLink(source);  
  23.   
  24.         }  
  25.         private void extractLink(HtmlDocument source) {  
  26.   
  27.             HtmlElementCollection anchorList = source.GetElementsByTagName("a");  
  28.   
  29.             foreach(var item in anchorList) {  
  30.                 checkedListBox.Items.Add(((HtmlElement) item).GetAttribute("href"));  
  31.             }  
  32.         }  
  33.     }  
  34. }  
Visual Studio

Conclusion

In this blog, we learned about creating a link extractor and filter in C#.