How do I select elements with a specific class using GoQuery?

GoQuery is a library for Go (Golang) that brings a syntax and a set of features similar to jQuery to the Go language. It's often used for web scraping and for working with HTML documents. To select elements with a specific class using GoQuery, you would typically use the Find method along with a selector that targets the class you're interested in.

Below is an example of how to use GoQuery to select elements with a specific class:

package main

import (
    "fmt"
    "log"
    "net/http"

    "github.com/PuerkitoBio/goquery"
)

func main() {
    // Fetch the HTML document
    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 into GoQuery
    doc, err := goquery.NewDocumentFromReader(res.Body)
    if err != nil {
        log.Fatal(err)
    }

    // Find and print elements with the class 'my-class'
    doc.Find(".my-class").Each(func(index int, item *goquery.Selection) {
        fmt.Printf("Element #%d: %s\n", index, item.Text())
    })
}

In this example:

  1. We make an HTTP GET request to http://example.com.
  2. We check for errors and the HTTP status code.
  3. We create a GoQuery document from the response body using goquery.NewDocumentFromReader.
  4. We call the Find method on the document to find all elements that have the class my-class.
  5. For each found element, we print its text content using the Text method.

Note that .my-class is a CSS selector that selects elements with the class attribute containing my-class. GoQuery supports a wide range of CSS selectors, so you can use more complex selectors to narrow down your search if needed.

Make sure you have GoQuery installed before running this code. If you haven't installed it yet, you can get it using go get:

go get github.com/PuerkitoBio/goquery

Always remember to check the website's robots.txt and terms of service before scraping to ensure you are allowed to scrape their content and that you're following their use policy.

Related Questions

Get Started Now

WebScraping.AI provides rotating proxies, Chromium rendering and built-in HTML parser for web scraping
Icon