<pre lang="c#">
public static List<string> AllImages(string startURL)
{
return SpecificLinks(startURL, "//img", "src");
}
public static List<string> SpecificLinks(string startUrl, string elementSelector, string attributeSelector)
{
List<string> links = new List<string>();
HtmlWeb hw = new HtmlWeb();
HtmlDocument doc = hw.Load(startUrl);
HtmlNodeCollection docNodes;
try
{
docNodes = doc.DocumentNode.SelectNodes(elementSelector);
}
catch
{
docNodes = null;
}
if (docNodes != null)
{
foreach (HtmlNode link in doc.DocumentNode.SelectNodes(elementSelector))
{
string elementSource = link.GetAttributeValue(attributeSelector, "#");
if (!elementSource.Equals("#"))
{
try
{
Uri uri = new Uri(new Uri(startUrl), elementSource);
if (!elementSource.Equals(uri.ToString()))
elementSource = uri.ToString();
else
elementSource = "#";
}
catch (Exception)
{
elementSource = "#";
}
}
if (!elementSource.Equals("#"))
links.Add(elementSource);
}
}
return links;
}