goquery
The goquery
module provides a convenient way to parse and query HTML documents using CSS selectors. It is a wrapper around the github.com/PuerkitoBio/goquery (opens in a new tab) library, which implements features similar to jQuery for parsing and manipulating HTML.
Module
goquery(input ...object)
The goquery
module object itself is callable in order to provide a shorthand for creating a goquery document from various sources:
- A string input: Parses the given HTML string into a document.
- A byte slice input: Parses the HTML content from the given bytes.
- A file or reader input: Creates a document from the content of the file or reader.
This is equivalent to calling goquery.parse()
with the same arguments.
>>> doc := goquery("<html><body><h1>Hello World</h1></body></html>")
goquery.document()
>>> doc := goquery(bytes("<html><body><h1>Hello</h1></body></html>"))
goquery.document()
>>> f := file.open("page.html")
>>> doc := goquery(f)
goquery.document()
Functions
parse
parse(input) document
Creates a goquery document by parsing the given input. The input can be:
- A string containing HTML
- A byte slice containing HTML
- A file or reader object
>>> doc := goquery.parse("<html><body><h1>Hello World</h1></body></html>")
goquery.document()
>>> doc := goquery.parse(bytes("<html><body><h1>Hello</h1></body></html>"))
goquery.document()
>>> f := open("page.html")
>>> doc := goquery.parse(f)
goquery.document()
Types
document
The document object represents an HTML document and provides methods for finding and manipulating its contents.
Methods
find
find(selector string) selection
Finds elements in the document that match the given CSS selector and returns them as a selection.
>>> doc := goquery("<html><body><h1>Hello</h1><p>World</p></body></html>")
>>> doc.find("h1")
goquery.selection()
html
html() string
Returns the HTML content of the document.
>>> doc := goquery("<html><body><h1>Hello</h1></body></html>")
>>> doc.html()
"<html><head></head><body><h1>Hello</h1></body></html>"
text
text() string
Returns the text content of the document, with all HTML tags removed.
>>> doc := goquery("<html><body><h1>Hello</h1><p>World</p></body></html>")
>>> doc.text()
"HelloWorld"
selection
The selection object represents a set of HTML elements selected from a document. It provides methods for filtering, traversing, and manipulating these elements.
Methods
find
find(selector string) selection
Finds elements within the current selection that match the given CSS selector.
>>> doc := goquery("<div><p>First</p><p>Second</p></div>")
>>> div := doc.find("div")
>>> div.find("p")
goquery.selection()
attr
attr(name string) string
Returns the value of the specified attribute for the first element in the selection. Returns nil if the attribute doesn't exist.
>>> doc := goquery("<a href='https://example.com'>Example</a>")
>>> doc.find("a").attr("href")
"https://example.com"
html
html() string
Returns the HTML content of the first element in the selection.
>>> doc := goquery("<div><p>Hello <b>World</b></p></div>")
>>> doc.find("p").html()
"Hello <b>World</b>"
text
text() string
Returns the combined text content of all elements in the selection.
>>> doc := goquery("<div><p>Hello <b>World</b></p><p>Example</p></div>")
>>> doc.find("p").text()
"Hello WorldExample"
each
each(func(index int, selection selection) object) object
Iterates over each element in the selection and calls the provided function with the index and a selection containing only that element.
>>> doc := goquery("<ul><li>Item 1</li><li>Item 2</li><li>Item 3</li></ul>")
>>> doc.find("li").each(func(i, s) { print(i, s.text()) })
0 Item 1
1 Item 2
2 Item 3
eq
eq(index int) selection
Returns a selection containing only the element at the specified index within the current selection.
>>> doc := goquery("<ul><li>Item 1</li><li>Item 2</li><li>Item 3</li></ul>")
>>> doc.find("li").eq(1).text()
"Item 2"
length
length() int
Returns the number of elements in the selection.
>>> doc := goquery("<ul><li>Item 1</li><li>Item 2</li><li>Item 3</li></ul>")
>>> doc.find("li").length()
3
first
first() selection
Returns a selection containing only the first element in the current selection.
>>> doc := goquery("<ul><li>Item 1</li><li>Item 2</li><li>Item 3</li></ul>")
>>> doc.find("li").first().text()
"Item 1"
last
last() selection
Returns a selection containing only the last element in the current selection.
>>> doc := goquery("<ul><li>Item 1</li><li>Item 2</li><li>Item 3</li></ul>")
>>> doc.find("li").last().text()
"Item 3"
parent
parent() selection
Returns a selection containing the parent elements of each element in the current selection.
>>> doc := goquery("<div><p><span>Text</span></p></div>")
>>> span := doc.find("span")
>>> span.parent().html()
"<span>Text</span>"
children
children(selector ...string) selection
Returns a selection containing the children of each element in the current selection, optionally filtered by a selector.
>>> doc := goquery("<div><p>First</p><span>Second</span><p>Third</p></div>")
>>> div := doc.find("div")
>>> div.children().length()
3
>>> div.children("p").length()
2
filter
filter(selector string) selection
Returns a filtered selection containing only the elements that match the given selector.
>>> doc := goquery("<div><p class='a'>First</p><p>Second</p><p class='a'>Third</p></div>")
>>> paragraphs := doc.find("p")
>>> paragraphs.filter(".a").length()
2
not
not(selector string) selection
Returns a filtered selection containing only the elements that do not match the given selector.
>>> doc := goquery("<div><p class='a'>First</p><p>Second</p><p class='a'>Third</p></div>")
>>> paragraphs := doc.find("p")
>>> paragraphs.not(".a").length()
1
has_class
has_class(class string) bool
Returns whether the first element in the selection has the given class.
>>> doc := goquery("<div class='container main'><p>Content</p></div>")
>>> div := doc.find("div")
>>> div.has_class("container")
true
>>> div.has_class("sidebar")
false