Skip to content

Instantly share code, notes, and snippets.

@lucacervasio
Last active August 29, 2015 14:26
Show Gist options
  • Save lucacervasio/f1e99401a2c13d87d229 to your computer and use it in GitHub Desktop.
Save lucacervasio/f1e99401a2c13d87d229 to your computer and use it in GitHub Desktop.

Revisions

  1. lucacervasio revised this gist Aug 8, 2015. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion google_search.go
    Original file line number Diff line number Diff line change
    @@ -69,7 +69,6 @@ func GoogleSearch(query string) (result []Result, totResult int, filteredResult
    //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
  2. lucacervasio created this gist Aug 8, 2015.
    91 changes: 91 additions & 0 deletions google_search.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,91 @@
    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)
    }