Skip to content

Instantly share code, notes, and snippets.

@adrianmoses
Last active April 20, 2018 03:13
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save adrianmoses/7655322 to your computer and use it in GitHub Desktop.
Save adrianmoses/7655322 to your computer and use it in GitHub Desktop.
goquery example
package main
/*
* Script that scrapes google front page
* Usage: ./google [<query>]
* e.g. ./google hacker news
*/
import (
"fmt"
"net/http"
"net/url"
"io"
"os"
"strings"
"github.com/PuerkitoBio/goquery"
)
func checkError(err error){
if err != nil {
panic(err)
os.Exit(1)
}
}
func main() {
query := strings.Join(os.Args[1:], "+")
response, err := http.Get("http://google.com/search?q=" + query)
checkError(err)
defer response.Body.Close()
doc, err := goquery.NewDocumentFromReader(io.Reader(response.Body))
checkError(err)
doc.Find("h3.r a").Each(func(i int, s *goquery.Selection) {
str, exists := s.Attr("href")
if exists {
u, err := url.Parse(str)
checkError(err)
m, _ := url.ParseQuery(u.RawQuery)
fmt.Println("\033[1;35m"+s.Text()+"\033[0m", m["q"][0])
} else {
fmt.Println(s.Text())
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment