如何将所有p/div标记写入txt文件
How I can write all the p/div tags to txt file
我需要编写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");
}
相关文章:
- 如何将 txt 文件中的行分隔为组件C++
- 在CMakeLists.txt的安装功能中使用.cmake文件有什么用
- 一种在C++中读取TXT配置文件的简单方法
- 无法打开.txt文件
- 如何使用CLion在Mac上创建一个新的.txt文件
- 我可以读取静态对象中的文件.txt吗?C++
- C++-需要读取存储在文件(.txt)中的时间戳并正确检索
- 具有文件.txt的 C++ 中的设置
- 如何在 CMakeList 中包含 h/hpp 头文件.txt而不是 cpp 或其他 hpp 文件
- 使用函数读取文件.txt中的矩阵
- 任何人都知道如何解决尝试将从MySQL数据库返回的行打印到文本文件(.txt)中的C++
- 读取文件.txt C
- 为什么我的方法不从文件.txt中读取float类型
- 如何读取名称(仅文件.txt)作为参数传递的文件(在任何目录中)?C++
- 读取文件".txt"并将每行的数据分配给循环列表中的新节点C++
- 如何用C++将字符串文件Txt解析成数组
- 如何在c++中读取和编辑文件txt
- 从文件.txt读取字符串数组并使用它来玩康威的生命游戏
- 导入文件txt时出错
- 如何仅读取文件.txt中的数字