Created
October 11, 2014 12:57
-
-
Save ericchiang/995261bfe4a01ba603a0 to your computer and use it in GitHub Desktop.
pup - Streaming parser test
This file contains hidden or 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 ( | |
"fmt" | |
"io" | |
"log" | |
"net/http" | |
"code.google.com/p/go.net/html" | |
) | |
func RecursiveParse(r io.Reader, context *html.Node, level int) { | |
fmt.Println("Parsing at level:", level) | |
nodes, err := html.ParseFragment(r, context) | |
if err == nil { | |
for _, node := range nodes { | |
fmt.Printf("%v", node) | |
RecursiveParse(r, node, level+1) | |
} | |
} else { | |
fmt.Println(err) | |
} | |
} | |
func main() { | |
resp, err := http.Get("https://html.spec.whatwg.org/") | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer resp.Body.Close() | |
RecursiveParse(resp.Body, nil, 0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment