Parsing HTML in C++: Library Overview and Practical Examples
Parsing HTML in C++ is a task that developers encounter when creating web scrapers, content analyzers, or data extraction tools. Unlike Python, where BeautifulSoup and lxml are the standard, C++ has no single dominant solution. However, there are several mature and performant libraries that allow efficient work with HTML documents.
In this article, we will look at three of the most popular libraries for parsing HTML in C++: Gumbo-parser (from Google), libxml2 (with its HTML module), and MyHTML. We will provide code examples, compare their capabilities, and give recommendations for choosing one.
1. Gumbo-parser: Reliability from Google
Gumbo-parser is a library from Google that implements HTML5 parsing in accordance with the WHATWG specification. It is written in pure C, making it lightweight and fast. Key advantages:
- Full HTML5 support (including invalid documents)
- Minimal dependencies
- Simple API for traversing the DOM tree
- High performance
Installation:
git clone https://github.com/google/gumbo-parsercd gumbo-parser./autogen.sh./configuremakesudo make installExample of parsing HTML and extracting all links:
#include <gumbo.h>#include <iostream>#include <string>#include <vector>
void extract_links(GumboNode* node, std::vector<std::string>& links) { if (node->type != GUMBO_NODE_ELEMENT) return;
GumboAttribute* href; if (node->v.element.tag == GUMBO_TAG_A && (href = gumbo_get_attribute(&node->v.element.attributes, "href"))) { links.push_back(href->value); }
const GumboVector* children = &node->v.element.children; for (unsigned int i = 0; i < children->length; ++i) { extract_links(static_cast<GumboNode*>(children->data[i]), links); }}
int main() { const std::string html = R"(<html><body> <a href="https://pythonlib.ru">PythonLib</a> <a href="/about">About Us</a> </body></html>)";
GumboOutput* output = gumbo_parse(html.c_str()); std::vector<std::string> links; extract_links(output->root, links);
for (const auto& link : links) { std::cout << "Found link: " << link << std::endl; }
gumbo_destroy_output(&kGumboDefaultOptions, output); return 0;}Output:
Found link: https://pythonlib.ruFound link: /about2. libxml2: Universal Parser with HTML Module
libxml2 is a powerful library for working with XML and HTML. Its HTML parser is based on the same principles as the XML parser but is adapted for the non-strict syntax of HTML. Advantages:
- XPath support for element selection
- Built-in encoding support
- Stability and widespread use
- Ability to work with XSLT
Installation (Ubuntu/Debian):
sudo apt-get install libxml2-dev libxslt1-devExample of using XPath to find all h2 headings:
#include <libxml/HTMLparser.h>#include <libxml/xpath.h>#include <iostream>
int main() { const char* html = "<html><body><h2>Heading 1</h2><h2>Heading 2</h2></body></html>"; htmlDocPtr doc = htmlReadDoc( reinterpret_cast<const xmlChar*>(html), nullptr, nullptr, HTML_PARSE_RECOVER | HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING );
xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc); xmlXPathObjectPtr Also in library
Working with YAML in JavaScript (Node.js): parsing, generation and validation
Internationalization (i18n) in C++: A Complete Guide
Elasticsearch Client for Java: Complete Guide to Connection and Work