Skip to content

Instantly share code, notes, and snippets.

@kjk
Created July 24, 2019 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kjk/804bca2d2dfc102f73d8817d4997d172 to your computer and use it in GitHub Desktop.
Save kjk/804bca2d2dfc102f73d8817d4997d172 to your computer and use it in GitHub Desktop.
chromedp bug
package main
import (
"context"
"fmt"
"net/url"
"time"
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/chromedp"
)
// $$("div.rc div.r").forEach((node) => console.log($("a", node).href))
// $$("div.rc > div:first-child > a:first-child").forEach((x) => console.log(x.href))
// $x('//div[@class="rc"]//div[@class="r"]//a[1]')
func spiderGoogle(searchTerm string) ([]string, error) {
//opts := append([]chromedp.ExecAllocatorOption{}, defaultOpts...)
//chromePath := `C:\Users\kjk\AppData\Local\documentalist\data\chrome-win\chrome.exe`
//opts = append(opts, chromedp.ExecPath(chromePath))
opts := chromedp.DefaultExecAllocatorOptions
ctx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
defer cancel()
ctx, cancel = chromedp.NewContext(
ctx,
//chromedp.WithLogf(fmtPrintf),
)
defer cancel()
// create a timeout
ctx, cancel = context.WithTimeout(ctx, 10*time.Second)
defer cancel()
qe := url.QueryEscape(searchTerm)
uri := fmt.Sprintf("https://www.google.com/search?q=" + qe)
fmt.Printf("before run, uri='%s'\n", uri)
sel := "div.rc > div:first-child > a:first-child"
var nodes []*cdp.Node
err := chromedp.Run(ctx,
chromedp.Navigate(uri),
// chromedp.WaitVisible(`foot`, chromedp.ByID),
chromedp.WaitVisible("#foot"),
chromedp.Nodes(sel, &nodes),
)
if err != nil {
return nil, err
}
var res []string
for _, node := range nodes {
href := node.AttributeValue("href")
res = append(res, href)
}
return res, nil
}
func main() {
res, err := spiderGoogle("krzysztof kowalczyk")
if err != nil {
fmt.Printf("spiderGoogle() failed with %s\n", err)
} else {
fmt.Printf("spiderGoogle returned %d results\n", len(res))
for _, s := range res {
fmt.Printf("%s\n", s)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment