how to get value of attribute href value in Go language

Puzzled Boy picture Puzzled Boy · Aug 23, 2015 · Viewed 9.4k times · Source

I want to parse anchor link from the html content. /* My HTML Content Sample

<a class="productnamecolor colors_productname" href="http://www.cccxcxc.com/Nautical-Bubble-Romper-p/s15brpnt03.htm">*/
      <a class="productnamecolor colors_productname" href="http://www.dewewe.com/Nautical-Bubble-Romper-p/erewrwer.htm">
    <a class="productnamecolor colors_productname" href="http://www.sdsddsd.com/Nautical-Bubble-Romper-p/dsadadasd.htm"> 

*/ The Anchor have href and i want to get the value of Href . But this is giving me error..

Error: multiple-value s.Attr() in single-value context

package main

    import (
      "fmt"
      "log"

      "github.com/PuerkitoBio/goquery"
    )

    func ExampleScrape() {
      doc, err := goquery.NewDocument("http://www.myurl.com/category-s/1828.htm") 
      if err != nil {
        log.Fatal(err)
      }

    /* **my sample html after http open** <a class="productnamecolor colors_productname" href="http://www.cccxcxc.com/Nautical-Bubble-Romper-p/s15brpnt03.htm">*/
      <a class="productnamecolor colors_productname" href="http://www.dewewe.com/Nautical-Bubble-Romper-p/erewrwer.htm">
    <a class="productnamecolor colors_productname" href="http://www.sdsddsd.com/Nautical-Bubble-Romper-p/dsadadasd.htm"> ***/

    doc.Find("table.v65-productDisplay a.productnamecolor").Each(func(i int, s *goquery.Selection) {
        band := s.Attr("href") // here i want to get attribute " href " value. this is not working here.
        fmt.Printf(band)
      })
    }

    func main() {
      ExampleScrape()
    }

Answer

Tim Cooper picture Tim Cooper · Aug 23, 2015

Selection.Attr returns two values: the attribute value, and a boolean stating whether the attribute existed or not (the attribute value will be the empty if this is false).

Go doesn't like it when you ignore multiple return values, so you'll have to change your code to the following:

doc.Find("table.v65-productDisplay a.productnamecolor").Each(func(i int, s *goquery.Selection) {
    band, ok := s.Attr("href")
    if ok {
        fmt.Printf(band)
    }
})