You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$10
For Premium members:
In this article you will learn how to create a link extractor using C#.
Introduction
In this article we will learn how to extract all the links from a webpage using a web client. At the end of this article you will be able to create an application that can extract links from pages and filter those links on the basis of parameters you want. So without wasting much time let's dive directly into the code.
Creating the Link Grabber
So we are creating a link grabber. For that we need some logic and it's always a good idea to clarify the logic before creating something. So let's define the logic.
The logic is:
Before proceeding let's code the preceding logic.
The Code
The following is the code for the grabber.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace linkGrabber
{
public partial class Form1 : Form
public Form1()
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
WebBrowser wb = new WebBrowser();
wb.Url = new Uri(textBox1.Text);
wb.DocumentCompleted += wb_DocumentCompleted;
void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
HtmlDocument source = ((WebBrowser)sender).Document;
extractLink(source);
private void extractLink(HtmlDocument source)
HtmlElementCollection anchorList = source.GetElementsByTagName("a");
foreach (var item in anchorList)
checkedListBox1.Items.Add(((HtmlElement)item).GetAttribute("href"));
Summary
That's it; all done. Now you have successfully made a link grabber. You can further extend it by adding a filter to it. In my next part I will show how to add a filter and how to download files. Thanks for reading and don't forget to comment and share.
Programming Dictionary in C#