goquery README

The project's README from github.com/PuerkitoBio/goquery. As one markdown file: README.md.

goquery - a little like that j-thing, only in Go

Build Status Go Reference Sourcegraph Badge

goquery brings a syntax and a set of features similar to jQuery to the Go language. It is based on Go's net/html package and the CSS Selector library cascadia. Since the net/html parser returns nodes, and not a full-featured DOM tree, jQuery's stateful manipulation functions (like height(), css(), detach()) have been left off.

Also, because the net/html parser requires UTF-8 encoding, so does goquery: it is the caller's responsibility to ensure that the source document provides UTF-8 encoded HTML. See the wiki for various options to do this.

Syntax-wise, it is as close as possible to jQuery, with the same function names when possible, and that warm and fuzzy chainable interface. jQuery being the ultra-popular library that it is, I felt that writing a similar HTML-manipulating library was better to follow its API than to start anew (in the same spirit as Go's fmt package), even though some of its methods are less than intuitive (looking at you, index()...).

Table of Contents

Installation

Required Go version:

Ongoing goquery development is tested on the latest 2 versions of Go.

$ go get github.com/PuerkitoBio/goquery

(optional) To run unit tests:

$ cd $GOPATH/src/github.com/PuerkitoBio/goquery
$ go test

(optional) To run benchmarks (warning: it runs for a few minutes):

$ cd $GOPATH/src/github.com/PuerkitoBio/goquery
$ go test -bench=".*"

Changelog

Note that goquery's API is now stable, and will not break.

API

goquery exposes two structs, Document and Selection, and the Matcher interface. Unlike jQuery, which is loaded as part of a DOM document, and thus acts on its containing document, goquery doesn't know which HTML document to act upon. So it needs to be told, and that's what the Document type is for. It holds the root document node as the initial Selection value to manipulate.

jQuery often has many variants for the same function (no argument, a selector string argument, a jQuery object argument, a DOM element argument, ...). Instead of exposing the same features in goquery as a single method with variadic empty interface arguments, statically-typed signatures are used following this naming convention:

Utility functions that are not in jQuery but are useful in Go are implemented as functions (that take a *Selection as parameter), to avoid a potential naming clash on the *Selection's methods (reserved for jQuery-equivalent behaviour).

The complete package reference documentation can be found here.

Please note that Cascadia's selectors do not necessarily match all supported selectors of jQuery (Sizzle). See the cascadia project for details. Also, the selectors work more like the DOM's querySelectorAll, than jQuery's matchers - they have no concept of contextual matching (for some concrete examples of what that means, see this ticket). In practice, it doesn't matter very often but it's something worth mentioning. Invalid selector strings compile to a Matcher that fails to match any node. Behaviour of the various functions that take a selector string as argument follows from that fact, e.g. (where ~ is an invalid selector string):

Examples

See some tips and tricks in the wiki.

Adapted from example_test.go:

package main

import (
  "fmt"
  "log"
  "net/http"

  "github.com/PuerkitoBio/goquery"
)

func ExampleScrape() {
  // Request the HTML page.
  res, err := http.Get("http://metalsucks.net")
  if err != nil {
    log.Fatal(err)
  }
  defer res.Body.Close()
  if res.StatusCode != 200 {
    log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)
  }

  // Load the HTML document
  doc, err := goquery.NewDocumentFromReader(res.Body)
  if err != nil {
    log.Fatal(err)
  }

  // Find the review items
  doc.Find(".left-content article .post-title").Each(func(i int, s *goquery.Selection) {
		// For each item found, get the title
		title := s.Find("a").Text()
		fmt.Printf("Review %d: %s\n", i, title)
	})
}

func main() {
  ExampleScrape()
}

Support

There are a number of ways you can support the project:

Buy Me A Coffee

License

The BSD 3-Clause license, the same as the Go language. Cascadia's license is here.

goquery (PuerkitoBio/goquery)

Searching Database…

Что это такое?goquery — это библиотека для Go, которая приносит синтаксис и возможности в стиле jQuery в разбор и обработку HTML. Она построена на пакете net/html из Go и библиотеке CSS-селекторов cascadia.
Я уже знаю jQuery — мне будет всё знакомо?Очень знакомо: названия функций и цепочки вызовов максимально повторяют jQuery. Разница в том, что «состояние», меняющее документ, хелперы вроде height(), css() и detach() не включены, потому что net/html возвращает узлы, а не полноценное DOM-дерево.
Как добавить её в свой проект?Выполните 'go get github.com/PuerkitoBio/goquery', импортируйте пакет, оберните свой HTML в Document — например, с помощью NewDocumentFromReader — и начинайте выбирать элементы через Find().
Какая версия Go мне нужна?goquery начиная с v1.12.0 требует Go 1.25+; более старые линейки выпусков требовали Go 1.24+, 1.23+ или 1.18+, а версии до v1.9 работали вплоть до Go 1.1.
Он разберёт любой HTML, который я ему дам?Только UTF-8: лежащий в основе парсер net/html требует кодировку UTF-8, поэтому позаботиться о том, чтобы исходный документ был в UTF-8, должен сам вызывающий; в вики описаны способы это сделать.
Где он работает?Это библиотека для Go, а не самостоятельное приложение: вы подключаете её к любой программе на Go — везде, где компилируется Go.
Почему автор сделал API по образцу jQuery, а не придумал что-то своё?Потому что jQuery сверхпопулярна, и автор счёл, что библиотеке для работы с HTML лучше следовать её API, чем начинать с нуля — в том же духе, в каком пакет fmt из Go повторяет привычные вещи, — вплоть до сохранения некоторых не самых интуитивных методов вроде index().
Почему у некоторых методов такие названия, как PrevFiltered или FilterNodes?Go — статически типизированный язык, поэтому вместо многочисленных перегрузок jQuery, упакованных в один метод с переменным числом аргументов, каждый вариант получает свой суффикс: XxxFiltered принимает строку-селектор, XxxSelection — Selection, XxxNodes — элементы DOM, XxxFunction — функцию, а XxxMatcher — скомпилированный matcher.
Все ли селекторы jQuery работают в goquery так же?Не совсем: селекторы cascadia не обязательно совпадают со всеми селекторами jQuery (Sizzle) и работают скорее как querySelectorAll из DOM, без учёта контекста. Некорректная строка-селектор молча компилируется в Matcher, который ничего не находит, поэтому Find('~') просто вернёт пустую выборку.
API стабильный? Проект ещё поддерживается?Да — в README сказано, что API стабилен и ломаться не будет. Разработка активная: в v1.13.0 от августа 2026 года добавлены улучшения производительности, функция верхнего уровня Text с опциями по образцу gettext из BeautifulSoup и go1.27 в матрицу тестов.
What are similar projects?cheerio, gjson, jsoup, go, colly, REDox, scrapy, webmagic
Size
0.7 MB
Stars
15K
Age
14y
Commits · 90 Days