Skip to content

htmlparser2vshtmlquery

MIT 12 4 4,789
277.1 million (month) Aug 28 2011 12.0.0(2026-03-20 23:08:40 ago)
781 1 8 MIT
Feb 07 2019 58.1 thousand (month) v1.3.6(2026-03-06 04:46:15 ago)

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.

htmlquery is a Go library that allows you to parse and extract data from HTML documents using XPath expressions. It provides a simple and intuitive API for traversing and querying the HTML tree structure, and it is built on top of the popular Goquery library.

Example Use


```javascript 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 = "

Hello, world!

"; parser.write(html); parser.end(); ```
```go package main import ( "fmt" "log" "github.com/antchfx/htmlquery" ) func main() { // Parse the HTML string doc, err := htmlquery.Parse([]byte(`

Hello, World!

  • Item 1
  • Item 2
  • Item 3
`)) if err != nil { log.Fatal(err) } // Extract the text of the first

element h1 := htmlquery.FindOne(doc, "//h1") fmt.Println(htmlquery.InnerText(h1)) // "Hello, World!" // Extract the text of all
  • elements lis := htmlquery.Find(doc, "//li") for _, li := range lis { fmt.Println(htmlquery.InnerText(li)) } // "Item 1" // "Item 2" // "Item 3" } ```
  • Alternatives / Similar


    Was this page helpful?