使用c / c ++以编程方式从html文件中提取表

时间:2013-02-09 20:52:22

标签: c++ html c xml-parsing html-parsing

我正在寻找从html文件中提取表格的更好的想法。现在我正在使用整洁(http://tidy.sourceforge.net/)将html文件转换为xhtml然后我使用rapidxml来解析xml。在解析时,我会查找<table><tr><td>个节点,从而创建我的表数据结构。

它工作得很好,但我想知道是否有更好的方法来完成我的任务。此外,整洁的lib似乎是一个废弃的项目。

还有人在整洁的源代码中尝试过“实验性”补丁吗?

谢谢, 基督教

2 个答案:

答案 0 :(得分:0)

我认为你的方法很好。我认为最好的是整理并将html转换为xhtml并解析xml。看不出它如何简化。

你没有提到任何问题,所以我不确定是什么问题。

答案 1 :(得分:0)

您可以使用htmlparser(https://github.com/HamedMasafi/htmlparser) 这个库可以解析,读取和修改html和css

例如,您要阅读表格


    html_parser html;
    html.set_text(html_text);
    auto table = html.query("#table_id").at(0);
    for (auto tr : table->childs()) {
        for (auto td : tr->childs()) {
            //now here you have a td and you are free to any modify are data read
            //e.g:
            auto td_tag = dynamic_cast<html_tag*>(td);
            td_tag->set_attr("id", "new_id"); // change attr
            auto id = td_tag->attr("id");
            auto test = td_tag->innser_text();
            auto html = td_tag->outter_html();
        }
    }

快速入门示例为here