-
-
Save alfonsodev/99f4685740081eb9cd7c to your computer and use it in GitHub Desktop.
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 | |
/* | |
* 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