Created
November 10, 2017 08:22
-
-
Save mortymacs/6eed55b0e07db86d81539b0a32fbf005 to your computer and use it in GitHub Desktop.
Sample HTML parser in golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Reference: https://schier.co/blog/2015/04/26/a-simple-web-scraper-in-go.html