如何将所有p/div标记写入txt文件

How I can write all the p/div tags to txt file

本文关键字:文件 txt div      更新时间:2023-10-16

我需要编写Qt/C++代码来提取所有的p标记,以将每个p标记写入.txt文件。例如,如果我有以下HTML页面:

        <!DOCTYPE html>
        <html>
         <body>
         <h1>My First Heading</h1>
         <p>My first paragraph.</p>
         <p>My second paragraph.</p>
         </body>
          </html>

我需要创建2.txt文件的代码,第一个文件将包括我的第一段。第二段将包括我的第二段。

我的问题是如何解析html并在标签之间获取txt,这里是我的代码

         int main(int argc, char *argv[])
           {
            QApplication a(argc, argv);
              QWebPage page;
              QWebFrame * frame =page.mainFrame();
               QUrl fileUrl ("https://en.wikipedia.org/wiki/Bank");
                frame->setUrl(fileUrl);
                QWebElement document = frame->documentElement();
            QWebElementCollection collection = document.findAll("p");
             foreach (QWebElement paraElement, collection) {
              }

                  MainWindow w;
                 w.show();
             return a.exec();
           }

非常感谢您的帮助

这是对您的需求的非常天真的实现。

#include <iostream>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <string>
#include <stack>
#include <queue>
#include <fstream>
#define BUFFER_SIZE 256 
//assuming flush means just printing 
//the <p> tags on the console.
void flushqueue(queue<string>& qtags){
  while(!qtags.empty()){
    cout <<std::endl << qtags.front() <<std::endl;
    qtags.pop();
  }
}
#define STAGSIZE 3
#define ETAGSIZE 4
const char *stag = "<p>";
const char *etag = "</p>";
void process(const string& instr,vector< pair<size_t,size_t> >& vectags){
    size_t  index = 0;
    pair<size_t,size_t>res(-1,-1);
    while(index !=string::npos  && index < instr.length()){
      res.first = -1;
      res.second = -1;
      index = instr.find(stag,index);
      if(index != string::npos){
    index+=STAGSIZE;
    res.first = index;
      }
      index = instr.find(etag,index);
      if(index != string::npos){
    res.second = index-1;
    index +=ETAGSIZE;
      }
      if(res.first !=-1 || res.second != -1)
    vectags.push_back(res);
    }
  }
int readptags(const char* filename){
  std::ifstream ifs (filename);
  queue<string>qbuffer;
  vector<pair<size_t,size_t> >vtags;
  string stag;
  while(ifs.good()){
    string temp;
    getline(ifs,temp);
    vtags.clear();
    process(temp,vtags);
    for(int i=0;i<vtags.size();i++){
      pair<size_t,size_t>res = vtags[i];
      if(res.first!=-1 && res.second !=-1){
    //a full string
    qbuffer.push(string(temp.begin()+res.first,temp.begin()+res.second+1));
      }else if(res.first==-1 && res.second !=-1){
    stag+=string(temp.begin(),temp.begin()+res.second+1) ;
    qbuffer.push(stag);
    stag.clear();
      }else if(res.first!=-1 && res.second ==-1){
    stag += string(temp.begin()+res.first,temp.end());
      }else{
    continue;
      }
    }
    if(qbuffer.size()>=BUFFER_SIZE)
      flushqueue(qbuffer);
  }
  flushqueue(qbuffer);
}
  int main(){
    //readptags("prac.html");
  }