Re: [go-nuts] Re: http "alt" attribute value for img tag?
package main import ( "fmt" "log" "strings" "golang.org/x/net/html" ) func main() { const src = ` message ` doc, err := html.Parse(strings.NewReader(src)) if err != nil { log.Fatal(err) } var f func(*html.Node) f = func(n *html.Node) { if n.Type == html.ElementNode && n.Data == "img" { for _, a := range n.Attr { if a.Key == "alt" { fmt.Printf("a.Key=%q a.Val=%q\n", a.Key, a.Val) break } } } for c := n.FirstChild; c != nil; c = c.NextSibling { f(c) } } f(doc) } -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[go-nuts] Re: http "alt" attribute value for img tag?
You should have a look at http://go-colly.org . I've done scraping with html.Parse before, and I wish Colly existed when I did. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[go-nuts] Re: http "alt" attribute value for img tag?
i am trying html Parse method from "html"* package but can't find element "img" ...* *Any ideas?* var f func(*html.Node) f = func(n *html.Node) { if n.Type == html.ElementNode { //&& n.Data == "alt" { for _, a := range n.Attr { //if a.Key == "img" { fmt.Printf("a.key=%s a.val=%s\n", a.Key, a.Val) break //} } } for c := n.FirstChild; c != nil; c = c.NextSibling { f(c) } } f(doc) -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[go-nuts] Re: http "alt" attribute value for img tag?
I would start by isolating the alt=“…” string with splits (https://golang.org/pkg/strings/#Split) then use Sscanf (https://golang.org/pkg/fmt/#Sscanf) to parse the number. There may be other approaches like regular expressions. Matt On Wednesday, April 18, 2018 at 9:52:28 PM UTC-5, l vic wrote: > > I need to get "alt" value from html "img" tag: > > > ... > message > > > > > > > > > > > What would be the correct way in Go to read "alt" values into array? > > Thank you, > > > > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.