Created
January 24, 2014 21:00
-
-
Save Veejay/8606301 to your computer and use it in GitHub Desktop.
Link fetcher/checker
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
import ( | |
"fmt" | |
/* "io/ioutil" */ | |
"net/http" | |
"code.google.com/p/go.net/html" | |
"strings" | |
) | |
func main() { | |
response, err := http.Get("http://www.autreplanete.com") | |
if err != nil { | |
// handle error | |
} | |
document, err := html.Parse(response.Body) | |
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" { | |
if strings.Contains(a.Val, "http://"){ | |
fmt.Printf("Fetching => %s", a.Val) | |
response, err := http.Get(a.Val) | |
if err != nil { | |
fmt.Printf("%v", response.StatusCode) | |
panic(err) | |
} | |
defer response.Body.Close() | |
if (response.StatusCode != http.StatusNotFound){ | |
fmt.Printf("%s %v\n\n\n\n", a.Val, response.StatusCode) | |
} | |
} | |
break | |
} | |
} | |
} | |
for c := n.FirstChild; c != nil; c = c.NextSibling { | |
f(c) | |
} | |
} | |
f(document) | |
} |
Author
Veejay
commented
Feb 7, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment