Skip to content

Instantly share code, notes, and snippets.

@mangar
Created November 14, 2017 14:27
Show Gist options
  • Save mangar/728c09a28b3acf808644f14157b2b214 to your computer and use it in GitHub Desktop.
Save mangar/728c09a28b3acf808644f14157b2b214 to your computer and use it in GitHub Desktop.
Screen Scraping in Go Lang
//
// Screen Scraping in Go Lang
//
// Lib:
// go get github.com/PuerkitoBio/goquery
//
package main
import (
"fmt"
"log"
"github.com/PuerkitoBio/goquery"
)
func postScrape() {
doc, err := goquery.NewDocument("https://github.com/PuerkitoBio/goquery")
if err != nil {
log.Fatal(err)
}
// use CSS selector found with the browser inspector
// for each, use index and item
doc.Find(".markdown-body h2").Each(func(index int, item *goquery.Selection) {
title := item.Text()
linkTag := item.Find("a")
link, _ := linkTag.Attr("href")
fmt.Printf("Post #%d: %s - %s\n", index, title, link)
})
}
func main() {
fmt.Println("> > > > > > > > > > > > > > > > > > > > > \n")
postScrape()
fmt.Println("\n< < < < < < < < < < < < < < < < < < < < < ")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment