【问题标题】:c# HtmlAgilityPack HTML parsing issuec# HtmlAgilityPack HTML解析问题
【发布时间】:2013-06-14 12:49:37
【问题描述】:

我有这个 html

<div class="postrow firs">
        <h2 class="title icon">
            This is the title
        </h2>
        <div class="content">
            <div id="post_message_1668079">
                <blockquote class="postcontent restore ">
                <div>Category</div>
                                         <div>Authour: Kim</div>
                    line 1<br /> line2
                </blockquote>
            </div>
        </div>
    </div>      <div class="postrow">
        <h2 class="title icon">
            This is the title
        </h2>
        <div class="content">
            <div id="post_message_1668079">
                <blockquote class="postcontent restore ">
                <div>Category</div>
                    line 1<br /> line2
                </blockquote>
            </div>
        </div>
    </div>

我想从每个具有“postrow”类的 div 中提取以下内容,并且可能还有其他类,例如 &lt;div class="postrow first"&gt;。所以“first”这个类不是我关心的,只需要在开头有“postrow”即可。

  1. 带有类标题的标签内的内容
  2. “blockquote”标签中的 HTML。但没有任何 div 与此 标记。

我试过的代码:

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            doc.LoadHtml("http://localhost/vanilla/");
            List<string> facts = new List<string>();
            foreach (HtmlNode li in doc.DocumentNode.SelectNodes("//div[@class='postrow']"))
            {
                facts.Add(li.InnerHtml);
                foreach (String s in facts)
                {
                    textBox1.Text += s + "/n";
                }
            }

【问题讨论】:

    标签: html-parsing


    【解决方案1】:

    您的代码有问题,您必须将 html 作为字符串而不是路径提供

    doc.LoadHtml("http://localhost/vanilla/");
    

    改为

    var request = (HttpWebRequest)WebRequest.Create("http://localhost/vanilla/");
    String response = request.GetResponse();
    
    doc.loadHtml(response);
    

    现在迭代解析的html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-04
      • 1970-01-01
      • 2022-08-23
      • 2015-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多