用C++获取HTML的纯文本
时间: 2024-03-15 17:47:09 浏览: 21
要获取HTML的纯文本,可以使用第三方库,如libxml2、pugixml等,它们都提供了HTML文本解析的功能。以下是一个使用pugixml库获取HTML纯文本的示例代码:
```c++
#include <iostream>
#include <pugixml.hpp>
int main()
{
std::string html = "<html><body><p>Hello, <strong>world</strong>!</p></body></html>";
pugi::html_document doc;
doc.load_string(html.c_str());
pugi::xpath_node_set nodes = doc.select_nodes("//text()");
for (pugi::xpath_node_set::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
std::cout << it->node().value() << std::endl;
}
return 0;
}
```
输出结果为:
```
Hello,
world
!
```
这样就成功获取到了HTML文本的纯文本部分。代码中,首先将HTML字符串加载到pugixml的html_document对象中,然后使用XPath表达式选取所有的文本节点,最后遍历所有文本节点并输出其值。
相关问题
用Tinyxml2实习获取html纯文本内容
Tinyxml2是一个轻量级的XML解析库,不能直接处理HTML文档。但可以通过其他的库,如libxml2来处理HTML文档,然后使用Tinyxml2来解析和处理解析后的文本内容。
下面给出一个获取HTML文本内容的示例代码:
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <libxml/HTMLparser.h>
#include "tinyxml2.h"
using namespace std;
using namespace tinyxml2;
void parseHtml(xmlNode *node, XMLDocument& doc);
int main()
{
// 读取HTML文件
string htmlFilePath = "example.html";
ifstream htmlFile(htmlFilePath);
if (!htmlFile.is_open()) {
cout << "Failed to open file: " << htmlFilePath << endl;
return 1;
}
string htmlContent((istreambuf_iterator<char>(htmlFile)), istreambuf_iterator<char>());
htmlFile.close();
// 解析HTML文档
HTMLParserCtxtPtr parserCtxt = htmlCreatePushParserCtxt(NULL, NULL, "", 0, "", XML_CHAR_ENCODING_NONE);
htmlParseChunk(parserCtxt, htmlContent.c_str(), htmlContent.size(), 0);
htmlParseChunk(parserCtxt, "", 0, 1);
xmlDocPtr docPtr = parserCtxt->myDoc;
xmlNode *rootNode = xmlDocGetRootElement(docPtr);
// 使用Tinyxml2解析文本内容
XMLDocument doc;
parseHtml(rootNode, doc);
cout << doc.FirstChildElement("body")->GetText() << endl;
// 释放资源
xmlFreeDoc(docPtr);
htmlFreeParserCtxt(parserCtxt);
xmlCleanupParser();
xmlMemoryDump();
return 0;
}
void parseHtml(xmlNode *node, XMLDocument& doc) {
for (xmlNode *curNode = node; curNode; curNode = curNode->next) {
if (curNode->type == XML_TEXT_NODE) {
doc.InsertEndChild(doc.NewText((const char *)curNode->content));
}
else if (curNode->type == XML_ELEMENT_NODE && strcmp((const char *)curNode->name, "script") != 0) {
XMLElement *newNode = doc.NewElement((const char *)curNode->name);
for (xmlAttr *attr = curNode->properties; attr; attr = attr->next) {
newNode->SetAttribute((const char *)attr->name, (const char *)attr->children->content);
}
parseHtml(curNode->children, newNode->ToDocument());
doc.InsertEndChild(newNode);
}
}
}
```
该示例代码将HTML文档解析为一个DOM树,然后使用递归的方式将文本内容提取出来,并使用Tinyxml2构建新的XML文档。其中,在解析HTML文档时,排除了`<script>`标签,因为`<script>`标签中的内容不应该被解析为文本内容。
最终,可以使用`doc.FirstChildElement("body")->GetText()`获取HTML文档中`<body>`标签中的纯文本内容。
QWebEngineView获取html文字内容 c++代码
获取QWebEngineView中的HTML文本内容可以使用QWebEnginePage的runJavaScript()方法,该方法可以执行JavaScript语句并返回执行结果。以下是一个获取HTML文本内容的C++代码示例:
```cpp
#include <QCoreApplication>
#include <QWebEngineView>
#include <QWebEnginePage>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QWebEngineView view;
view.setHtml("<html><head><title>Test</title></head><body><h1>Hello World!</h1></body></html>");
QWebEnginePage* page = view.page();
page->toHtml([page](const QString& html) {
qDebug() << html;
page->deleteLater();
QCoreApplication::quit();
});
return a.exec();
}
```
在这个例子中,我们创建了一个QWebEngineView并设置了一些HTML内容。然后,我们获取QWebEngineView的QWebEnginePage,在QWebEnginePage的toHtml()方法中执行JavaScript语句,获取HTML文本内容并输出到控制台。最后,我们删除QWebEnginePage并退出应用程序。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)