API Reference

goquery

jQuery-like HTML parsing and manipulation library for Go built on the net/html tokenizer.

Official Documentation ↗
CompatibilityThis documentation covers version goquery v0.5 / Go 1.20+. Check the official docs for the latest API surface.

Installation

gogo get github.com/PuerkitoBio/goquery

Methods

Overview

goquery brings jQuery-style traversal and manipulation to Go. It parses HTML into a selection tree you can query with CSS selectors and read or modify node by node.

Core Methods

Parse with NewDocumentFromReader, select with Find, iterate with Each, and read with Attr, Text and Html. First, Eq and Parent refine selections, and chained selector calls narrow results.

Usage Notes

Write resilient selectors against stable attributes, verify Length before assuming a match, and trim extracted text. Combine with an HTTP client and rate limiting for scraping.

Common Pitfalls

Selector assumptions break when markup changes. goquery does not run JavaScript, so dynamic pages need rendering first. Parsing untrusted HTML is not the same as sanitizing it.

Errors

The following points are quoted verbatim from the official documentation. 来源:https://cdn.jsdelivr.net/gh/PuerkitoBio/goquery@latest/README.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:49:59.982Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.

FAQ

Does goquery execute JavaScript?
No. It parses the served HTML only, so client-rendered content is absent unless the page is server-rendered or you fetch it with a browser automation tool first.
How do I handle multiple matches?
A selection can match many elements. Use First, Eq or Each to operate on specific nodes, and check Length before indexing assumptions.
Is the parsed document safe for untrusted HTML?
goquery uses a HTML5-compliant parser that repairs invalid markup, which is robust but not a sanitizer. Sanitize HTML before rendering it back to users.