在这里,我试图阅读网址并在页面中获取图像.我需要排除页面,如果它是404并停止从404错误页面获取图像.如何使用
HtmlAgilityPack?这是我的代码
var document = new HtmlWeb().Load(completeurl); var urls = document.DocumentNode.Descendants("img") .Select(e => e.GetAttributeValue("src",null)) .Where(s => !String.IsNullOrEmpty(s)).ToList();
解决方法
您需要在HtmlWeb实例上注册PostRequestHandler事件,它将在每个下载的文档之后引发,您将可以访问HttpWebResponse对象.它具有StatusCode的属性.
HtmlWeb web = new HtmlWeb(); HttpStatusCode statusCode = HttpStatusCode.OK; web.PostRequestHandler += (request,response) => { if (response != null) { statusCode = response.StatusCode; } } var doc = web.Load(completeUrl) if (statusCode == HttpStatusCode.OK) { // received a read document }
查看GutHub上HtmlAgilityPack的代码,它甚至更简单,HtmlWeb有一个属性StatusCode,其值设置为:
var web = new HtmlWeb(); var document = web.Load(completeurl); if (web.StatusCode == HttpStatusCode.OK) { var urls = document.DocumentNode.Descendants("img") .Select(e => e.GetAttributeValue("src",null)) .Where(s => !String.IsNullOrEmpty(s)).ToList(); }
更新
AgilityPack API已有更新.诀窍仍然是一样的:
var htmlWeb = new HtmlWeb(); var lastStatusCode = HttpStatusCode.OK; htmlWeb.PostResponse = (request,response) => { if (response != null) { lastStatusCode = response.StatusCode; } };