Last active
August 29, 2015 14:26
-
-
Save lucacervasio/f1e99401a2c13d87d229 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
import ( | |
"fmt" | |
"log" | |
"net/http" | |
"net/url" | |
"strings" | |
"github.com/PuerkitoBio/goquery" | |
) | |
type Result struct { | |
Title string | |
Url string | |
Rank int | |
Query string | |
} | |
func GoogleSearch(query string) (result []Result, totResult int, filteredResult int) { | |
client := &http.Client{} | |
req, err := http.NewRequest("GET", "https://www.google.it/search?q="+url.QueryEscape(query), nil) | |
if err != nil { | |
log.Fatalln(err) | |
} | |
req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Ge cko) Chrome/44.0.2403.130 Safari/537.36") | |
resp, err := client.Do(req) | |
if err != nil { | |
log.Fatalln(err) | |
} | |
defer resp.Body.Close() | |
doc, err := goquery.NewDocumentFromReader(resp.Body) | |
if err != nil { | |
log.Fatal(err) | |
} | |
skip := []string{ | |
"tripadvisor.it", | |
"elenchi.com", | |
"youtube.com", | |
"reteimprese.it", | |
"icitta.it", | |
"impresaitalia.info", | |
"cercoimprese.com", | |
"elenchitelefonici.it", | |
"aziendit.com", | |
"paginegialle.it", | |
"misterimprese.it", | |
"facebook.com", | |
"paginebianche.it", | |
"virgilio.it", | |
"telefonare.it", | |
"aziendix.com", | |
"paginemail.it", | |
"trovanumeri.com", | |
"infobel.com", | |
"plus.google.com", | |
} | |
doc.Find("h3.r a").Each(func(i int, s *goquery.Selection) { | |
totResult += 1 | |
link, _ := s.Attr("href") | |
title := s.Text() | |
//fmt.Printf("Review %d: %s - %s\n", i, title, link) | |
found := false | |
for _, s := range skip { | |
if strings.Contains(link, s) { | |
found = true | |
filteredResult += 1 | |
} | |
} | |
if !found { | |
result = append(result, Result{title, link, i + 1, query}) | |
} | |
}) | |
return | |
} | |
func main() { | |
res, tot, fil := GoogleSearch("query") | |
fmt.Println(res) | |
fmt.Println(tot) | |
fmt.Println(fil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment