Last active
August 29, 2015 14:16
-
-
Save nataren/8cdc4fc594b12c443468 to your computer and use it in GitHub Desktop.
Go crawler
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 ( | |
"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