Getting src attribute from img tag
I'm using the HAP library to parse HTML: http://html-agility-pack.net
I basically just want to ret开发者_如何转开发rieve the src
value from all the img
tags.
I've tried several thing but I can't seem to do it!
Modified from the examples page:
HtmlDocument doc = new HtmlDocument();
doc.Load("file.htm"); //or whatever HTML file you have
HtmlNodeCollection imgs = doc.DocumentNode.SelectNodes("//img[@src]");
if (imgs == null)
return;
foreach (HtmlNode img in imgs)
{
if (img.Attributes["src"] == null)
continue;
HtmlAttribute src = img.Attributes["src"];
//Do something with src.Value
}
Did you try something like this:
HtmlNodeCollection images = doc.DocumentNode.SelectNodes("//img[@src]");
精彩评论