Created
November 14, 2017 14:27
-
-
Save mangar/728c09a28b3acf808644f14157b2b214 to your computer and use it in GitHub Desktop.
Screen Scraping in Go Lang
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
// | |
// 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