Skip to content

Instantly share code, notes, and snippets.

@mortymacs
Created November 10, 2017 08:22
Show Gist options
  • Save mortymacs/6eed55b0e07db86d81539b0a32fbf005 to your computer and use it in GitHub Desktop.
Save mortymacs/6eed55b0e07db86d81539b0a32fbf005 to your computer and use it in GitHub Desktop.
Sample HTML parser in golang
package main
import (
"net/http"
"golang.org/x/net/html"
// "io/ioutil"
"fmt"
)
func main() {
resp, _ := http.Get("http://mortezana.com")
// bytes, _ := ioutil.ReadAll(resp.Body)
// fmt.Println(string(bytes))
z := html.NewTokenizer(resp.Body)
for {
tt := z.Next()
switch {
case tt == html.ErrorToken:
return
case tt == html.StartTagToken:
t := z.Token()
if t.Data == "div" {
for _, a := range t.Attr {
if a.Key == "class" {
fmt.Println("Data", a.Val)
break
}
}
}
}
}
}
@mortymacs
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment