# htmlCpp **Repository Path**: ChenYLhuman/htmlCpp ## Basic Information - **Project Name**: htmlCpp - **Description**: c++ / cpp 的 html 解析库( 支持 部分 xpath) - **Primary Language**: C++ - **License**: Apache-2.0 - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 4 - **Forks**: 1 - **Created**: 2024-04-22 - **Last Updated**: 2025-08-27 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # htmlCpp 基于 C++ / cpp 实现的 html 解析库 加入 xpath查找(类 xpath) ## 环境 C++ camake ## 主要调用 ### 静态函数 ```c++ /// /// 根据字符串内容生成节点列表 /// /// 指向字符串的指针 /// 结束下标 /// 开始下标,最终下标 /// 解析后的列表 static HtmlDoc HtmlTools::HtmlDoc::parse( const std::shared_ptr< std::wstring > std_c_w_string, size_t &end_index, size_t &start_index ); ``` ### 成员函数 ```c++ /// /// 本对象关联的节点解析兄弟关系 /// /// 解析完成的节点列表 Vector_HtmlNodeSPtr_Shared HtmlTools::HtmlDoc::analysisBrotherNode( ); ``` ```c++ /// /// 遍历获取节点
/// 节点传递到 callFun 调用参数当中,当需要存储时,返回 true。直到结束 ///
/// 校验函数 /// 返回列表 Vector_HtmlNodeSPtr_Shared HtmlTools::HtmlDoc::getNodes( const std::function< bool( const HtmlNode_Shared &node ) > &callFun ); ``` ## 库 ### cylHtmlTools::XPath 实现 xptah路径查找 #### 首要路径 ../ ​ 上级目录 ./ ​ 当前目录 any ​ 任意节点,将会搜索任意名称为 any 节点名称的节点 / ​ 根节点,返回根部节点 #### 案例 //body ​ 根节点下的 body 节点 ./body ​ 当前节点中搜索所有友邻节点,并且返回 bdoy 节点 ../body ​ 父级节点当中搜索 body 节点 div ​ 所有名为 div 的节点 div[@class='hd'] ​ 所有属性为 class=‘hd’ 的 div 节点 div[@class='hd cf']/div 所有拥有属性为 class=‘hd’ 的 div 父节点的 div 节点 ​ # 测试调用 ## testHtmlCpp #### void testXAttribute( const cylHtmlTools::HtmlString &test_paremt_name, const cylHtmlTools::HtmlString &test_paremt_value ) ```c++ /// /// 测试 XDirAttribute /// /// XDirAttribute 名称 /// XDirAttribute 值 void testXAttribute( const cylHtmlTools::HtmlString &test_paremt_name, const cylHtmlTools::HtmlString &test_paremt_value ); ``` ```c++ void testXAttribute( const cylHtmlTools::HtmlString &test_paremt ) { testXAttribute( test_paremt, test_paremt ); } ``` ```c++ testXAttribute( LR"(@class="23 31" 123 " 3 11 ")" ); testXAttribute( LR"(@acd="23 31" 123 " 3 11 ")" ); ``` ##### 显示 ``` =============== 找到名称: "class" 找到值: "23" 找到值: "31" 找到值: "123" 找到值: "3" 找到值: "11" =============== =============== 找到名称: "acd" 找到值: "23 31" 找到值: "123" 找到值: " 3 11 " =============== ``` #### void testXAttributeIsIncludeOther( const cylHtmlTools::HtmlString &test_paremt_name, const cylHtmlTools::HtmlString &test_paremt_value, const std::vector< cylHtmlTools::HtmlString > &value ); ```c++ /// /// 测试属性对象是否包含指定属性值 /// /// 用于构建 xdir 对象的名称参数 /// 用于构建 xdir 对象的值参数 /// 用于匹配被包含的值列表 void testXAttributeIsIncludeOther( const cylHtmlTools::HtmlString &test_paremt_name, const cylHtmlTools::HtmlString &test_paremt_value, const std::vector< cylHtmlTools::HtmlString > &value ); ``` ```c++ void testXAttributeIsIncludeOther( const cylHtmlTools::HtmlString &test_paremt, const std::vector< cylHtmlTools::HtmlString > &value ) { testXAttributeIsIncludeOther( test_paremt, test_paremt, value ); } ``` ```c++ testXAttributeIsIncludeOther( LR"(@acd="23 31" 123 " 3 11 ")", { LR"(23)" } ); testXAttributeIsIncludeOther( LR"(@acd="23 31" 123 " 3 11 ")", { LR"(123)" } ); testXAttributeIsOtherInclude( LR"(@acd="23 31" 123 " 3 11 ")", { LR"(23 31)" , LR"(123)" , LR"( 3 11 )" , LR"(23)" , LR"(777)" } ); testXAttributeIsOtherInclude( LR"(@acd="23 31" 123 " 3 11 ")", { LR"(23 31)" , LR"( 3 11 )" , LR"(23)" , LR"(777)" } ); ``` ##### 显示 ``` =============== 测试 XDirAttribute 包含其他值列表 找到名称: "acd" 找到值: "23 31" 找到值: "123" 找到值: " 3 11 " 输出值列表 : "23" 查找值列表 : 不包含 =============== =============== 测试 XDirAttribute 包含其他值列表 找到名称: "acd" 找到值: "23 31" 找到值: "123" 找到值: " 3 11 " 输出值列表 : "123" 查找值列表 : 包含 =============== =============== 测试其他包含 XDirAttribute 值列表 找到名称: "acd" 找到值: "23 31" 找到值: "123" 找到值: " 3 11 " 输出值列表 : "23 31", "123", " 3 11 ", "23", "777" 查找值列表 : 包含 =============== =============== 测试其他包含 XDirAttribute 值列表 找到名称: "acd" 找到值: "23 31" 找到值: "123" 找到值: " 3 11 " 输出值列表 : "23 31", " 3 11 ", "23", "777" 查找值列表 : 不包含 =============== ``` #### void testXDir( const cylHtmlTools::HtmlString &test_paremt ) ```c++ /// /// 测试 XDir 对象 /// /// 生成 XDir 对象的参数 void testXDir( const cylHtmlTools::HtmlString &test_paremt ) ; ``` ```c++ testXDir( LR"(div[@"id"="sitebox sd" @class="cf ds"])" ); ``` ##### 显示 ``` =============== 获得路径名称:"div" 获得属性名称:"id" 获得属性值:"sitebox" 获得属性值:"sd" 获得属性名称:"class" 获得属性值:"cf" 获得属性值:"ds" =============== ``` #### void testXPath( const cylHtmlTools::HtmlString &test_paremt ) ```c++ /// /// 测试 XPath 对象 /// /// xpath 对象的参数 void testXPath( const cylHtmlTools::HtmlString &test_paremt ); ``` ```c++ testXPath( LR"(div[@"id"="sitebox" @class="cf"])" ); ``` ##### 显示 ``` =============== 获取路径 : "div[@id=sitebox @class=cf]" 找到目录名称: "div" 获得路径名称:"div" 获得属性名称:"id" 获得属性值:"sitebox" 获得属性名称:"class" 获得属性值:"cf" =============== ``` #### void testHtmlNodeAttributeConverToXDirAttribute( const cylHtmlTools::HtmlString &parem ) ```c++ /// /// 测试:把一个 node 节点转换到 XDirAttribute /// /// void testHtmlNodeAttributeConverToXDirAttribute( const cylHtmlTools::HtmlString &parem ); ``` ```c++ testHtmlNodeAttributeConverToXDirAttribute( LR"(id="sitebox" class="cf")" ); testHtmlNodeAttributeConverToXDirAttribute( LR"(id="sitebox" class="cf de")" ); testHtmlNodeAttributeConverToXDirAttribute( LR"(id="sitebox bs" class="cf de")" ); testHtmlNodeAttributeConverToXDirAttribute( LR"(id="sitebox bs" sitebox1 bs2 class="cf de")" ); testHtmlNodeAttributeConverToXDirAttribute( LR"(id="sitebox bs" sitebox1 bs2 class="cf de" cf3 de4)" ); ``` ##### 显示 ``` =============== 测试节点属性转换到 Vector_XDirAttributeSPtr_Shared 转换:"id="sitebox" class="cf"" 获得属性名称:"id" 获得属性值:"sitebox" 获得属性名称:"class" 获得属性值:"cf" =============== =============== 测试节点属性转换到 Vector_XDirAttributeSPtr_Shared 转换:"id="sitebox" class="cf de"" 获得属性名称:"id" 获得属性值:"sitebox" 获得属性名称:"class" 获得属性值:"cf" 获得属性值:"de" =============== =============== 测试节点属性转换到 Vector_XDirAttributeSPtr_Shared 转换:"id="sitebox bs" class="cf de"" 获得属性名称:"id" 获得属性值:"sitebox" 获得属性值:"bs" 获得属性名称:"class" 获得属性值:"cf" 获得属性值:"de" =============== =============== 测试节点属性转换到 Vector_XDirAttributeSPtr_Shared 转换:"id="sitebox bs" sitebox1 bs2 class="cf de"" 获得属性名称:"id" 获得属性值:"sitebox" 获得属性值:"bs" 获得属性值:"sitebox1" 获得属性值:"bs2" 获得属性名称:"class" 获得属性值:"cf" 获得属性值:"de" =============== =============== 测试节点属性转换到 Vector_XDirAttributeSPtr_Shared 转换:"id="sitebox bs" sitebox1 bs2 class="cf de" cf3 de4" 获得属性名称:"id" 获得属性值:"sitebox" 获得属性值:"bs" 获得属性值:"sitebox1" 获得属性值:"bs2" 获得属性名称:"class" 获得属性值:"cf" 获得属性值:"de" 获得属性值:"cf3" 获得属性值:"de4" =============== ```