Skip to content

Instantly share code, notes, and snippets.

@nataren
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nataren/8cdc4fc594b12c443468 to your computer and use it in GitHub Desktop.
Save nataren/8cdc4fc594b12c443468 to your computer and use it in GitHub Desktop.
Go crawler
package main
import (
"log"
"github.com/PuerkitoBio/goquery"
"strings"
)
func main() {
hostname := "https://news.ycombinator.com"
doc, err := goquery.NewDocument(hostname)
if err != nil {
log.Fatal(err)
}
homePageLinks := 0
doc.Find("a").Each(func(i int, s *goquery.Selection) {
homePageLinks++
uri, _ := s.Attr("href")
var newUri string
if !strings.HasPrefix(uri, "http") {
newUri = hostname + uri
} else {
newUri = uri
}
})
log.Printf("Found %v links", homePageLinks)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment