使用HTMLAgilityPack非常容易。
var w = new HtmlWeb();
var doc = w.Load("http://www.explosm.net/comics/3104/");
var imgNode = doc.DocumentNode.SelectSingleNode("//*[@id=\"maincontent\"]/div[2]/div[2]/div[1]/img");
var src = imgNode.GetAttributeValue("src", "");
该变量src
将具有值http://www.explosm.net/db/files/Comics/Matt/Dont-be-a-dickhead.png
。
您所要做的就是下载图像:
var request = (HttpWebRequest)WebRequest.Create(src);
var response = request.GetResponse();
var stream = response.GetResponseStream();
//Here you have an Image object
Image img = Image.FromStream(stream);
//And you can save it or do whatever you want
img.Save(@"C:\file.png");