I want to build a List of images from a website link and then later also to save/download the images to the hard disk.
This is how im getting the links from a website/url:
private List<string> getLinks(HtmlAgilityPack.HtmlDocument document)
{
List<string> mainLinks = new List<string>();
var linkNodes = document.DocumentNode.SelectNodes("//a[@href]");
if (linkNodes != null)
{
foreach (HtmlNode link in linkNodes)
{
var href = link.Attributes["href"].Value;
if (href.StartsWith("http://") ==true || href.StartsWith("https://") ==true || href.StartsWith("www.") ==true) // filter for http
{
mainLinks.Add(href);
}
}
}
return mainLinks;
}
Then im using in this test function to build a List of all the links from the url:
private List<string> test(string url, int levels, DoWorkEventArgs eve)
{
HtmlWeb hw = new HtmlWeb();
List<string> webSites;
try
{
doc = hw.Load(url);
webSites = getLinks(doc);
//retriveImages();
So webSites wich is a List will have all the link from the main website url fro example if its google.com then in webSites i will have 19 items each one is a link from google.com
And retrieveImages() is:
private void retrieveImages()
{
var nodes = doc.DocumentNode.SelectNodes("//img");
foreach (var node in nodes)
{
List<string> images = new List<string>();
images.Add(node.Name);
}
}
And retrieveImages() for sure is not good code and also is not working. If im using it moving the // and calling the retrieveImages() nothing happened webSites List is empty.
What i want to do is in the function retrieveImages() to build a list of images from the curretn site/link im in now and then also to download the images to the hard disk the images in the List i built.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…