Saturday, January 01, 2005

Extracting URLS from Website and binding to datagrid

Here is a small snippet that you can use to get the links from a certain URL.


WebClient objWebClient = new WebClient();
aRequestHTML = objWebClient.DownloadData(strUrl);
UTF8Encoding utf8 = new UTF8Encoding();
myString = utf8.GetString(aRequestHTML);

Regex r = new Regex("href\\s*=\\s*(?:(?:\\\"(?[^\\\"]*)\\\")|(?[^\\s]* ))");
MatchCollection mcl = r.Matches(myString);
Response.Write(r.ToString());

foreach(Match ml in mcl)
{
foreach(Group g in ml.Groups)
{
string b = g.Value + "
";
a.Add(b);


}
}

DataGrid1.DataSource = a;
DataGrid1.DataBind();

No comments: