Meget groft eksempel på hvordan det kan gøres:
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
namespace e723908
{
class Program
{
static void Main(string[] args)
{
string url = "
http://www.eksperten.dk/spm/723908"; string html = GetHtml(url);
string[] links = GetLinks(html);
foreach (string link in links)
Console.WriteLine(link);
}
private static string GetHtml(string url)
{
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
return sr.ReadToEnd();
}
private static Regex linkMatcher = new Regex(@"href=""(.+?)""", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static string[] GetLinks(string html)
{
List<string> links = new List<string>();
Match match = linkMatcher.Match(html);
while (match.Success)
{
links.Add(match.Groups[1].Value);
match = match.NextMatch();
}
return links.ToArray();
}
}
}