从html获取图像的问题 [英] problem with get image from html

查看:66
本文介绍了从html获取图像的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


这个代码我试图获取图像,它给了我一些图像,但在那之后告诉我这个:对象引用未设置为对象的实例。

 HtmlAgilityPack.HtmlDocument html_doc =  new  HtmlAgilityPack.HtmlDocument(); 
foreach var i in rssFeed)
{
// ------------ --------- links ---------------------- //
html_doc.LoadHtml(i.Encoded。的ToString());
var link_html = html_doc.DocumentNode.SelectNodes( //一个);
foreach var link in link_html)
{
if (link.Attributes [ href]。Value.IndexOf( 。jpg> -1)
{
image_links.Add(link.Attributes [ href]。Value);
}
}

解决方案

看起来link_html中的某些锚点节点没有有href-Tags(link.Attributes [href])。这可能会生成您在foreach期间获得的NullReferenceException。



在使用之前检查Value是否已设置。

  String  .IsNullOrEmpty(link.Attributes [ 

Hi on this code i've tried to get images,it give me some image but after that show me this :Object reference not set to an instance of an object.

HtmlAgilityPack.HtmlDocument html_doc = new HtmlAgilityPack.HtmlDocument();
foreach (var i in rssFeed)
{
    //---------------------links----------------------//
    html_doc.LoadHtml(i.Encoded.ToString());
    var link_html = html_doc.DocumentNode.SelectNodes("//a");
    foreach (var link in link_html)
    {
            if (link.Attributes["href"].Value.IndexOf(".jpg") > -1)
            {
                image_links.Add(link.Attributes["href"].Value);
            }
    }

解决方案

Looks like some of the anchor-nodes in link_html do not have "href"-Tags (link.Attributes["href"]). This might generate the NullReferenceException you are getting during the foreach.

Check if Value is set before using it.

String.IsNullOrEmpty(link.Attributes["href"])

for example


这篇关于从html获取图像的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆