【问题标题】:Parsing incorrect html with HtmlAgilityPack使用 HtmlAgilityPack 解析不正确的 html
【发布时间】:2012-01-18 13:47:51
【问题描述】:

我正在尝试解析以下 HTML:

<li>
    <b style="font-size: 11px;">News:</b>
    <ul>
        <li><a style="font-size: 11px;" title="Program 1">program1</a></li>
        <li><a style="font-size: 11px;" title="Program 2">program2</a></li>
        <li><a style="font-size: 11px;" title="Program 3">program3</a></li>
    </ul>
</li>

<li>
    <b style="font-size: 11px;">Cartoons:</b>
    <ul>
        <li><a style="font-size: 11px;" title="Program 4">program4</a></li>
        <li><a style="font-size: 11px;" title="Program 5">program5</a></li>
        <li><a style="font-size: 11px;" title="Program 6">program6</a></li>
    </ul>
</li>

<li>
    <b style="font-size: 11px;">Music:</b>
    <ul>
        <li><a style="font-size: 11px;" title="Program 7">program7</a></li>
        <li><a style="font-size: 11px;" title="Program 8">program8</a></li>
        <li><a style="font-size: 11px;" title="Program 9">program9</a></li>                     
    </ul>
</li>

我想得到的是程序的类型(内部文本)和程序的名称(内部文本)。

我想把结果放在下面的对象中:

public class Programs
{
   public string Name { get; set; }
   public string Category { get; set; }
}

这是我当前的代码:

var content = from li in document.DocumentNode.Descendants("li")
                          from b in li.Descendants("b")
                          from a in li.Descendants("a")
                          where a.Attributes["title"] != null && a.Attributes["title"].Value.StartsWith("Program")
                          select new Channel
                          {
                              Name = a.InnerText,
                              Category = b.InnerText
                          };

            foreach (Programs c in content)
            {
                string s = c.Name;
                string ss = c.Category;
            }

但对于每个节目,我只获得“新闻”类别。
我该怎么做才能获得正确的类别? (新闻 - 节目 1、节目 2、节目 3,卡通 - 节目 4、节目 5、节目 6...)。

【问题讨论】:

    标签: c# linq html-parsing html-agility-pack


    【解决方案1】:

    嗯,奇怪。刚刚下载了最新的HtmlAgilityPack 并尝试了您的示例,它给了我正确的结果:

    News:: program1
    News:: program2
    News:: program3
    Cartoons:: program4
    Cartoons:: program5
    Cartoons:: program6
    Music:: program7
    Music:: program8
    Music:: program9
    

    我的测试代码是:

     var htmlDoc = new HtmlDocument();
     htmlDoc.Load("test.txt");
     var items = from li in htmlDoc.DocumentNode.Descendants("li")
                 from b in li.Descendants("b")
                 from a in li.Descendants("a")
                 where a.Attributes["title"] != null && 
                 a.Attributes["title"].Value.StartsWith("Program")
                 select new
                 {
                   Name = a.InnerText,
                   Category = b.InnerText
                 };
    
     foreach (var item in items)
     {
       Console.WriteLine("{0}: {1}", item.Category, item.Name);
     }
     Console.ReadKey();
    

    我猜你在你的代码中遗漏了一些简单的东西。 Btw 'test.txt' 文件包含上面的 html。

    【讨论】:

    • 嗯,调查了一下,我发现我得到了所有新闻类别的节目,然后是纸箱类别的所有节目,然后又是音乐类别的所有节目......奇怪
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-26
    • 2016-11-20
    • 2010-12-03
    • 2017-04-22
    • 1970-01-01
    • 2016-07-04
    相关资源
    最近更新 更多