c++解析html

https://github.com/cejutue/cjhtmlparser

 

基於gumbo-parser 和gumbo-query 改造成爲了 可以適用 Windows和Linux的 html解析庫, 原gumbo爲google的開源c庫, 且僅僅hi喫Linux.

編譯: gumbo-parser 所有文件全部直接加入到工程直接編譯即可

基本用法如下:

#pragma once
#include "stdafx.h"

#include "enumtest.cpp"

#include "gumbo-parser/Selector.h"

#include "gumbo-parser/Document.h"
#include "gumbo-parser/Selection.h"
#include "gumbo-parser/Node.h"

void test_parser() {
	std::string page("<h1><a>wrong link</a><a class=\"special\"\\>some link</a></h1>");
	CDocument doc;
	doc.parse(page.c_str());

	CSelection c = doc.find("h1 a.special");
	CNode node = c.nodeAt(0);
	printf("Node: %s\n", node.text().c_str());
	std::string content = page.substr(node.startPos(), node.endPos() - node.startPos());
	printf("Node: %s\n", content.c_str());

}

void test_html() {
	std::string page = "<html><div><span>1\n</span>2\n</div></html>";
	CDocument doc;
	doc.parse(page.c_str());
	CNode pNode = doc.find("div").nodeAt(0);
	std::string content = page.substr(pNode.startPos(), pNode.endPos() - pNode.startPos());
	printf("Node: #%s#\n", content.c_str());
}

void test_escape() {
	std::string page = "<html><div><span id=\"that's\">1\n</span>2\n</div></html>";
	CDocument doc;
	doc.parse(page.c_str());
	CNode pNode = doc.find("span[id=\"that's\"]").nodeAt(0);
	std::string content = page.substr(pNode.startPos(), pNode.endPos() - pNode.startPos());
	printf("Node: #%s#\n", content.c_str());
}

int main() {
	test_parser();
	test_html();
	test_escape();
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章