---
title: "Parse HTML and query CSS selectors in Go"
url: https://recursiveai.org/onehtml/go/cookbook/parse-html-and-query-css-selectors/
description: "Parse an HTML document in Go, select matching elements with a CSS selector, and read their text without launching a browser or running JavaScript."
version: "1.1.1"
---

# Parse HTML and query CSS selectors in Go

**onehtml-go** is a free, open-source HTML parser for Go from Recursive AI, with CSS selectors, computed styles, lossless editing and accessibility checks. MIT or Apache-2.0, no dependencies.

[pkg.go.dev](https://pkg.go.dev/recursiveai.org/go/onehtml) · [GitHub](https://github.com/recursive-ai-oss/onehtml-go) · [Docs](https://recursiveai.org/onehtml/go/docs/) · [Download example (.zip)](https://recursiveai.org/onehtml/go/cookbook/parse-html-and-query-css-selectors/parse-html-and-query-css-selectors.zip)

Parse the HTML, then call `doc.Select("#menu > li.featured")` to find matching elements without a browser. Requires Go 1.24 or later.

```shell
go get recursiveai.org/go/onehtml
```

```go
package main

import (
	"fmt"
	"log"
	"strings"

	"recursiveai.org/go/onehtml"
)

func main() {
	const html = `<ul id="menu">
  <li class="featured">Atlas</li>
  <li>Compass</li>
  <li class="featured">Map</li>
</ul>`
	doc, err := onehtml.Parse(strings.NewReader(html))
	if err != nil {
		log.Fatal(err)
	}
	items, err := doc.Select("#menu > li.featured")
	if err != nil {
		log.Fatal(err)
	}
	for _, item := range items {
		fmt.Println(item.TextContent())
	}
}
```

Paste the code above into main.go, run `go mod init example` and `go get recursiveai.org/go/onehtml`, then `go run .`; or unzip [the example](https://recursiveai.org/onehtml/go/cookbook/parse-html-and-query-css-selectors/parse-html-and-query-css-selectors.zip) and `go run .`.

```text
Atlas
Map
```

Only the featured items directly under `#menu` are printed, in document order. A selector that cannot be compiled returns an error; a valid selector with no matches returns no items.

## Variations

For just the first match, replace the selection and loop. `doc` is the parsed document from the example.

```go
item, err := doc.SelectFirst("#menu > li.featured")
if err != nil {
	log.Fatal(err)
}
if item.Valid() {
	fmt.Println(item.TextContent())
}
```

This prints `Atlas`.

## Related recipes

- [Extract links and link text from HTML in Go](https://recursiveai.org/onehtml/go/cookbook/extract-links-and-anchor-text/)
- [Inspect CSS declarations after the cascade](https://recursiveai.org/onehtml/go/cookbook/find-invalid-css-computed-declarations/)

## The short answer

The matches come back in document order. `TextContent()` gets their text. onehtml-go does not run scripts that might add to the HTML later.

## How do you parse an HTML page in Go?

Call [`onehtml.Parse`](https://pkg.go.dev/recursiveai.org/go/onehtml@v1.1.1#Parse) with the page’s HTML in an `io.Reader`. The example uses `strings.NewReader` so you can run it without a network request. An open file or the body of a successful HTTP response is also a reader. Your code opens and closes that input; parsing only builds the document from those bytes. If you’re parsing a template from disk to check its structure, use its generated HTML rather than template source with placeholders.

Selectors work on the tree that the parser builds, not on the source text. The [HTML parsing rules](https://html.spec.whatwg.org/multipage/parsing.html#tree-construction) can add implied elements or close an element before the next one begins. A direct-child query follows those parent-child relationships. When a selector copied from a template yields no results, check whether its assumed structure matches the parsed tree.

## How do you get an element’s value using a CSS selector in Go?

Choose the scope and then the kind of value you need. In `#menu > li.featured`, `#menu` identifies the list, `>` requires an immediate child, and `li.featured` selects a list item with the class `featured`. If you remove the class condition, `Compass` also matches. If you replace `>` with a space, featured list items in nested lists can match too. These are differences in what nodes match; neither query changes the HTML or applies CSS declarations.

Call `TextContent()` for descendant text, or [`Get`](https://pkg.go.dev/recursiveai.org/go/onehtml@v1.1.1#Node.Get) for an attribute’s value and its presence flag. `SelectFirst` gives you a single match, with a zero node signaling no match. If you only need to inspect one row, you don’t need to collect every match or index a slice that might be empty.

## Why does a CSS selector work in a browser but return nothing in Go?

Start by comparing the input with what the browser shows. onehtml-go does not fetch anything or run JavaScript, so a product list inserted by client-side code is absent unless your input already contains it. Browser interaction is another difference: a selector such as `:hover` relies on caller-supplied state in Go, not on a live pointer.

A bad selector and a missing element are different cases. Check the query error first, then check whether any nodes matched. If you run the same selector against many documents, compile it once with [`CompileSelector`](https://pkg.go.dev/recursiveai.org/go/onehtml@v1.1.1#CompileSelector) and use the compiled selector’s `All` method with each document root. For a single query, `Select` handles both steps.
