I’m using HtmlAgilityPack.
In this function the imageNodes in the foreach count is 0
I don’t understand why the list count is 0
The website contains many images. What I want is to get a list of the images from the site and show the list in the richTextBox1 and I also want to save all the images from the site on my hard disk.
How can I fix it ?
public void GetAllImages()
{
// Bing Image Result for Cat, First Page
string url = "http://www.bing.com/images/search?q=cat&go=&form=QB&qs=n";
// For speed of dev, I use a WebClient
WebClient client = new WebClient();
string html = client.DownloadString(url);
// Load the Html into the agility pack
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
// Now, using LINQ to get all Images
List<HtmlNode> imageNodes = null;
imageNodes = (from HtmlNode node in doc.DocumentNode.SelectNodes("//img")
where node.Name == "img"
&& node.Attributes["class"] != null
&& node.Attributes["class"].Value.StartsWith("img_")
select node).ToList();
foreach (HtmlNode node in imageNodes)
{
// Console.WriteLine(node.Attributes["src"].Value);
richTextBox1.Text += node.Attributes["src"].Value + Environment.NewLine;
}
}
As I can see the correct class of the Bing images is
sg_t. You can obtain thoseHtmlNodeswith the following Linq query:This list should be filled with all the
imgwithclass = 'sg_t'