2
package main

import (
    "fmt"
    "github.com/PuerkitoBio/goquery"
    "net/http"
)

func main() {

    url := "https://books.toscrape.com"

    resp, err := http.Get(url)

    if err != nil {
        fmt.Println(err)
    }

    defer resp.Body.Close()

    doc, err := goquery.NewDocumentFromReader(resp.Body)

    if err != nil {
        fmt.Println(err)
    }

    doc.Find(".product_pod").Each(func(i int, selection *goquery.Selection) {
        title := selection.Find(".image_container").Find("img")
        price := selection.Find(".price_color").Text()
        fmt.Printf("Title: %s, Price: %s\n", title, price)
    })
}

I am trying to use goquery to extract the Title from https://books.toscrape.com, I already extracted the prices, but I am having trouble extracting the title since it has multiple attributes.

This is how you print it with the colly library Except, I'm not trying to do it with the colly, I'm trying to do it with goquery. Here is the website: https://books.toscrape.com

   title := e.ChildAttr(".image_container img", "alt")
})```
Woody
  • 21
  • 2

1 Answers1

0

You can use Selection.Attr("alt") to select alt attribute of the current element:

doc.Find(".product_pod").Each(func(i int, selection *goquery.Selection) {
    title, _ := selection.Find(".image_container img").Attr("alt")
    price := selection.Find(".price_color").Text()
    fmt.Printf("Title: %s, Price: %s\n", title, price)
})

Note that Attr() returns 2 values: the attribute value and boolean wether the value exists (for some weird reason), so you need to unpack the values as in the example above (the title, _ = part)

Granitosaurus
  • 20,530
  • 5
  • 57
  • 82