Skip to content


MIT 14 2 656
58.1 thousand (month) Jun 08 2019 (2 months ago)
4,304 4 14 MIT
Aug 28 2011 120.7 million (month) 9.1.0(5 months ago)

xpath is a library for Go that allows you to use XPath expressions to select elements from an HTML document. It is built on top of the html package in the Go standard library, and provides a way to select elements from an HTML document using XPath expressions, which are more powerful and expressive than CSS selectors.

htmlparser2 is a Node.js library for parsing HTML and XML documents. It works by building a tree of elements, similar to the Document Object Model (DOM) in web browsers. This allows you to easily traverse and manipulate the structure of the document.

htmlparser2 is a low-level html tree parser but it can still be useful in web scraping as it's a powerful tool for HTML restructuring and serialization.

Example Use

package main

import (

func main() {
  // Create an HTML string
  html := `<html>
          <div id="content">
            <p>Hello, World!</p>
            <a href="">Example</a>

  // Parse the HTML string into a node tree
  doc, err := html.Parse(strings.NewReader(html))
  if err != nil {
    fmt.Println("Error:", err)

  // Compile the XPath expression
  expr, err := xpath.Compile("//p")
  if err != nil {
    fmt.Println("Error:", err)

  // Use the Evaluate method to select elements from the document
  nodes, err := expr.Evaluate(xpath.NodeNavigator(doc))
  if err != nil {
    fmt.Println("Error:", err)
  if nodes.MoveNext() {
    // > Hello, World!
const htmlparser = require("htmlparser2");
const parser = new htmlparser.Parser({
    onopentag: (name, attribs) => {
        console.log(`Opening tag: ${name}`);
    ontext: (text) => {
        console.log(`Text: ${text}`);
    onclosetag: (name) => {
        console.log(`Closing tag: ${name}`);
}, {decodeEntities: true});

const html = "<p>Hello, <b>world</b>!</p>";

Alternatives / Similar

Was this page helpful?