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) documentCreates 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) selectionFinds 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() stringReturns 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() stringReturns 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) selectionFinds 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) stringReturns 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() stringReturns 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() stringReturns 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) objectIterates 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 3eq
eq(index int) selectionReturns 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() intReturns 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()
3first
first() selectionReturns 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() selectionReturns 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() selectionReturns 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) selectionReturns 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()
2filter
filter(selector string) selectionReturns 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()
2not
not(selector string) selectionReturns 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()
1has_class
has_class(class string) boolReturns 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