GoQuery is a library for Go (Golang) that provides a set of functions and methods to parse and manipulate HTML, similar to jQuery. If you want to extract an attribute value from an HTML element using GoQuery, you'll first have to parse the HTML document and then use the appropriate selector to target the element. Once you have the element, you can retrieve the attribute value using the Attr
method.
Here's a step-by-step guide on how to do this:
- Install GoQuery: If you haven't already installed GoQuery, you can do so using the following command:
go get github.com/PuerkitoBio/goquery
- Import GoQuery: In your Go code, import the GoQuery package:
import (
"github.com/PuerkitoBio/goquery"
"log"
"net/http"
)
- Load the HTML document: You can load an HTML document from a URL, a file, or a string. Here's how to load it from a URL:
// Request the HTML page.
res, err := http.Get("http://example.com/")
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 element and extract the attribute: Use the
Find
method to locate the HTML element and thenAttr
to get the attribute's value.
// Find the element and get the attribute
var attributeValue string
doc.Find("selector").Each(func(i int, s *goquery.Selection) {
// For example, if you want to extract the "href" attribute
href, exists := s.Attr("href")
if exists {
attributeValue = href
// Do something with the attribute value, like printing it
log.Println("The href attribute is:", href)
}
})
Replace "selector"
with the actual CSS selector that matches the HTML element you're interested in. And replace "href"
with the name of the attribute you want to retrieve.
Here's a full example that extracts the href
attribute of the first link (<a>
tag) in an HTML document:
package main
import (
"github.com/PuerkitoBio/goquery"
"log"
"net/http"
)
func main() {
// Request the HTML page.
res, err := http.Get("http://example.com/")
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 element and get the attribute
doc.Find("a").Each(func(i int, s *goquery.Selection) {
// For example, if you want to extract the "href" attribute
href, exists := s.Attr("href")
if exists {
// Do something with the attribute value, like printing it
log.Println("The href attribute is:", href)
}
})
}
Make sure to handle errors and edge cases as necessary for your specific use case.