日期:2014-05-17  浏览次数:20463 次

提取文章中的图片

在Article中content字段存储了 新闻,需要重每篇新闻中提取一张图片(如果存在图片的话),这样可以图片当成新闻的封面,请问是不是要用正则,请问该怎么写?

谢谢







------解决方案--------------------
内容中可能包含多张图片,用正则可能取到的是一个小图标..
一般是用单独的字段保存图片
------解决方案--------------------
HtmlAgilityPack


string strHtml = "Your html string";
HtmlAgilityPack.HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(strHtml);
HtmlNodeCollection imgs = doc.DocumentNode.SelectNodes("img");
string src = String.Empty;
if (imgs.Count > 0)
{
    src = imgs[0].Attributes["src"].Value;
}