Skip to content

Instantly share code, notes, and snippets.

@frank0718
Forked from adrianmoses/goquery-example.go
Created October 13, 2016 11:17
Show Gist options
  • Save frank0718/e1532f8303fbb8602c9ef23b997d9d13 to your computer and use it in GitHub Desktop.
Save frank0718/e1532f8303fbb8602c9ef23b997d9d13 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