2017-01-28 02:14:38 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
cdp "github.com/knq/chromedp"
|
|
|
|
cdptypes "github.com/knq/chromedp/cdp"
|
|
|
|
"github.com/knq/chromedp/client"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// create context
|
|
|
|
ctxt, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// create chrome
|
2017-02-14 08:41:23 +00:00
|
|
|
c, err := cdp.New(ctxt, cdp.WithTargets(client.New().WatchPageTargets(ctxt)), cdp.WithLog(log.Printf))
|
2017-01-28 02:14:38 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// run task list
|
|
|
|
var site, res string
|
|
|
|
err = c.Run(ctxt, googleSearch("site:brank.as", "Easy Money Management", &site, &res))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("saved screenshot of #testimonials from search result listing `%s` (%s)", res, site)
|
|
|
|
}
|
|
|
|
|
|
|
|
func googleSearch(q, text string, site, res *string) cdp.Tasks {
|
|
|
|
var buf []byte
|
|
|
|
sel := fmt.Sprintf(`//a[text()[contains(., '%s')]]`, text)
|
|
|
|
return cdp.Tasks{
|
|
|
|
cdp.Navigate(`https://www.google.com`),
|
|
|
|
cdp.Sleep(2 * time.Second),
|
|
|
|
cdp.WaitVisible(`#hplogo`, cdp.ByID),
|
2017-01-29 03:37:56 +00:00
|
|
|
cdp.SendKeys(`#lst-ib`, q+"\n", cdp.ByID),
|
|
|
|
cdp.WaitVisible(`#res`, cdp.ByID),
|
2017-01-28 02:14:38 +00:00
|
|
|
cdp.Text(sel, res),
|
|
|
|
cdp.Click(sel),
|
|
|
|
cdp.Sleep(2 * time.Second),
|
|
|
|
cdp.WaitVisible(`#footer`, cdp.ByQuery),
|
|
|
|
cdp.WaitNotVisible(`div.v-middle > div.la-ball-clip-rotate`, cdp.ByQuery),
|
|
|
|
cdp.Location(site),
|
|
|
|
cdp.Screenshot(`#testimonials`, &buf, cdp.ByID),
|
2017-02-12 04:59:33 +00:00
|
|
|
cdp.ActionFunc(func(context.Context, cdptypes.Handler) error {
|
2017-01-28 02:14:38 +00:00
|
|
|
return ioutil.WriteFile("testimonials.png", buf, 0644)
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|