【问题标题】:How can I parse this HTML to get the content I want?如何解析这个 HTML 以获得我想要的内容?
【发布时间】:2012-06-30 07:55:37
【问题描述】:

我目前正在尝试解析 HTML 文档以检索其中的所有脚注;该文件包含数十个。我真的不知道用来提取我想要的所有内容的表达式。问题是,类(例如“calibre34”)在每个文档中都是随机的。查看脚注所在位置的唯一方法是搜索“隐藏”,然后它总是文本,并用 标记关闭。下面是 HTML 文档中脚注之一的示例,我想要的只是文本。有任何想法吗?谢谢大家!

<td class="calibre33">1.<span><a class="x-xref" href="javascript:void(0);">
[hide]</a></span></td>
<td class="calibre34">
Among the other factors on which the premium would be based are the
average size of the losses experienced, a margin for contingencies,
a loading to cover the insurer's expenses, a margin for profit or
addition to the insurer's surplus, and perhaps the investment
earnings the insurer could realize from the time the premiums are
collected until the losses must be paid.</td>

【问题讨论】:

  • 用什么解析? I hope you don't mean Regex... 用你用来解析 HTML 的语言标记你的帖子,否则没人能帮助你。
  • 您能找到a 类的x-ref 标记并获取最近的td 父级吗?
  • 使用 XDocument (XML to LINQ) 或 XmlDocument (POCO) 来解析您的 HTML。这两个 XML 库都已包含在 .NET/C# 中,并且非常健壮。
  • 没有 td 元素怎么办?他们一样吗?我的意思是,脚注,它总是在同一个 td 元素中吗?如果你想用java解析,你可以去jericho html parser
  • 所有脚注都在 td 标签中,但许多其他内容也是如此。这些 HTML 文档非常庞大,其中包含大量内容和标签,它们写得非常糟糕,我的工作就是把脚注拿出来,我只是不想坐在那里复制粘贴它们 30 年。另外,谢谢SpikeX,我去看看。

标签: c# html parsing


【解决方案1】:

使用HTMLAgilityPack 加载 HTML 文档,然后使用此 XPath 提取脚注:

//td[text()='[hide]']/following-sibling::td

基本上,它所做的是首先选择所有包含[hide]td 节点,然后最后去选择它们的下一个兄弟节点。所以下一个td。一旦你有了这个节点集合,你就可以提取它们的内部文本(在 C# 中,在 HtmlAgilityPack 中提供支持)。

【讨论】:

  • 谢谢哥们,我会试试这个,让你知道它是如何工作的。
  • @OriginJM:别提了。它应该可以正常工作。如果没有,请告诉我,我会尝试调整它。基本思路是对的。
  • 嘿伙计,我终于开始测试了,效果很好。谢谢,你就是男人!
【解决方案2】:

使用 MSHTML 解析 HTML 源代码如何? 这是演示代码。享受吧。

public class CHtmlPraseDemo
{
    private string strHtmlSource;
    public mshtml.IHTMLDocument2 oHtmlDoc;
    public CHtmlPraseDemo(string url)
    {
        GetWebContent(url);
        oHtmlDoc = (IHTMLDocument2)new HTMLDocument();
        oHtmlDoc.write(strHtmlSource);
    }
    public List<String> GetTdNodes(string TdClassName)
    {
        List<String> listOut = new List<string>();
        IHTMLElement2 ie = (IHTMLElement2)oHtmlDoc.body;
        IHTMLElementCollection iec = (IHTMLElementCollection)ie.getElementsByTagName("td");
        foreach (IHTMLElement item in iec)
        {
            if (item.className == TdClassName)
            {
                listOut.Add(item.innerHTML);
            }
        }
        return listOut;
    }
    void GetWebContent(string strUrl)
    {
        WebClient wc = new WebClient();
        strHtmlSource = wc.DownloadString(strUrl);
    }



}

class Program
{
 static void Main(string[] args)
    {
        CHtmlPraseDemo oH = new CHtmlPraseDemo("http://stackoverflow.com/faq");

        Console.Write(oH.oHtmlDoc.title);
        List<string> l = oH.GetTdNodes("x");
        foreach (string n in l)
        {
            Console.WriteLine("new td");
            Console.WriteLine(n.ToString());

        }

        Console.Read();
    }
}

【讨论】:

  • 我发现 mshtml 太可怕了。任何诸如
    之类的自结束标记绝对会破坏您的解析尝试。我目前正在寻找一种新的解析方法
猜你喜欢
  • 2011-08-06
  • 1970-01-01
  • 2017-04-20
  • 2016-07-29
  • 2011-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多