how to get value of attribute href value in Go language

13.6k Views Asked by At

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()
    }
2

There are 2 best solutions below

1
On BEST ANSWER

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)
    }
})
2
On

You can also use the golang.org/pkg/net/html package.

package main

import (
    "fmt"
    "log"
    "strings"

    "golang.org/x/net/html"
)

func main() {
    s := `<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, err := html.Parse(strings.NewReader(s))
    if err != nil {
        log.Fatal(err)
    }
    var f func(*html.Node)
    f = func(n *html.Node) {
        if n.Type == html.ElementNode && n.Data == "a" {
            for _, a := range n.Attr {
                if a.Key == "href" {
                    fmt.Println(a.Val)
                    break
                }
            }
        }
        for c := n.FirstChild; c != nil; c = c.NextSibling {
            f(c)
        }
    }
    f(doc)
}
// outputs
// http://www.cccxcxc.com/Nautical-Bubble-Romper-p/s15brpnt03.htm
// http://www.dewewe.com/Nautical-Bubble-Romper-p/erewrwer.htm
// http://www.sdsddsd.com/Nautical-Bubble-Romper-p/dsadadasd.htm

Hope this helps someone.