发布时间:2026-11-02 17:43:52 浏览次数:0
搜索相关内容:
说明:有些网站还有图片,广告等信息,但由于我们做的是Boost库的搜索,这些信息知道即可。
说明: 为了方便理解代码,博主将项目的目录进行贴出,因为下面我们include包含使用的库,使用的是绝对路径。
项目的路径为:/home/shun_hua/practical-projects/Boost_Search
原因:相对目录是基于进程的工作目录,进程的工作目录在项目的路径下,而我们写的代码是在项目的子目录的路径下,因此要么把所有的文件都放在项目的路径下,要么就用绝对路径,虽然长,但是可以把文件整理分类,看着比较简洁。
[shun_hua@iZ2zebfc5jur5cm0zu2n3gZ Boost_Search]$rz -E [shun_hua@iZ2zebfc5jur5cm0zu2n3gZ Boost_Search]$ l***oost_1_84_0.tar.gz[shun_hua@iZ2zebfc5jur5cm0zu2n3gZ Boost_Search]$ tar xzf boost* [shun_hua@iZ2zebfc5jur5cm0zu2n3gZ Boost_Search]$ l***oost_1_84_0 boost_1_84_0.tar.gz
说明:
[shun_hua@iZ2zebfc5jur5cm0zu2n3gZ Boost_Search]$ l***oost_1_84_0[shun_hua@iZ2zebfc5jur5cm0zu2n3gZ Boost_Search]$ cp -r ./boost_1_84_0/doc/html ./input [shun_hua@iZ2zebfc5jur5cm0zu2n3gZ Boost_Search]$ l***oost_1_84_0 input
说明:cp -r [指定路径的目录] [目标路径 + 重命名]
[shun_hua@iZ2zebfc5jur5cm0zu2n3gZ input]$ ls -R | grep -E ".html" | wc -l8586
说明:
基本框架:
引入文件库:Boost文件库,具体使用里面的filesystem里面的接口。
接口:文档
命名空间:boost::filesystem
类:class path
string string(const codecvt_type& cvt=codecvt()) const; ————将path对象转换为string类。path extension() const; ——文件的后缀。
迭代器:class recursive_directory_iterator
recursive_directory_iterator() noexcept;//默认构造,其实执向的是endexplicit recursive_directory_iterator(const path& p, directory_options opts\ = directory_options::none);//用根目录初始化,即整个多叉树根。
接口:
bool is_regular_file(const path& p); //判断是否是普通的文件,目录不是普通文件。bool exists(const path& p); //判断是否文件的目录是否存在。
bool GetPathFiles(const string& path,vector* files){ //首先将path转化为boost库的path便于处理 //防止命名污染的情况 namespace fs = boost::filesystem; fs::path root_path(path); if(!exists(root_path)) { lg(CRIT,"path is not exist!"); return false; } fs::recursive_directory_iterator end; for(fs::recursive_directory_iterator cur(root_path); cur != end; cur++) { //如果不是普通文件,例如目录。 if(!is_regular_file(cur->path())) { continue; } string suffix = cur->path().extension().string(); if(suffix != ".html") { continue; } string path = cur->path().string(); // cout path().string()); } return true;} 核心:
网页的大致内容:
说明:
解析文件的基本流程:
//使用命名空间,避免命名污染namespace util{ namespace filesystem { bool ReadFiles(const std::string& file_path,std::string *text) { std::ifstream fin(file_path); if(!fin.is_open()) { lg(WARNNING,"open file fail!"); return false; } string line; while(getline(fin,line)) { *text += line; } return true; } }} bool PraseTile(const string& text,string *title){ string prefix = ""; string suffix = ""; auto begin = text.find(prefix); auto end = text.find(suffix); if(begin == string::npos || end == string::npos) return false; begin += prefix.size(); *title = text.substr(begin,end - begin); return true;} bool PraseContent(const string& text,string *content){ State s = LABLE; for(char ch : text) { switch (s) { case LABLE: if(ch == '>') s = CONTENT; break; case CONTENT: if(ch == '