Skip to content

Instantly share code, notes, and snippets.

@clouedoc
Created July 15, 2018 11:11
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 clouedoc/014c3390500e45325deb47bcb5dec8dd to your computer and use it in GitHub Desktop.
Save clouedoc/014c3390500e45325deb47bcb5dec8dd to your computer and use it in GitHub Desktop.
Finally friendly got it right, thanks @ghetzel !
package main
import (
"fmt"
"github.com/ghetzel/go-webfriend"
"github.com/ghetzel/go-webfriend/browser"
"github.com/ghetzel/go-webfriend/commands/core"
"log"
"time"
)
const (
googleUrl string = "https://google.com/"
googleSearch string = "cute cats"
)
func main() {
var err error
// create the browser
chrome := browser.NewBrowser()
// modify the browser's options
chrome.Headless = false
// launch the browser & handle errors gracefully
if err = chrome.Launch(); err != nil {
log.Fatalf("An error happened while launching the browser: %s\n", err.Error())
}
// create the environement (with which we'll interact)
env := webfriend.NewEnvironment(chrome)
// go to the target web page
if _, err = env.Core.Go(googleUrl, &core.GoArgs{
Timeout: 10 * time.Second,
}); err != nil {
log.Fatalf("An error happened while loading %s: %v", googleUrl, err.Error())
}
// fill the target field
if _, err = env.Core.Field(`#lst-ib`, &core.FieldArgs{
Value: googleSearch,
Enter: true,
}); err != nil {
log.Fatalf("Error while interacting with field: %v", err.Error())
}
// find all the result links, by selector, with a timeout of 5 seconds.
var resultLinks []*browser.Element
if resultLinks, err = env.Core.Select(`h3.r a`, &core.SelectArgs{
CanBeEmpty: true, // avoid returning an error when there are no results
}); err != nil {
log.Fatalf("Couldn't scrape links: %v", err.Error())
}
// exit if we don't have a least one link
if len(resultLinks) < 1 { // we need at least one link
log.Fatal("No link found")
}
// loop through all the links and print them nicely
for _, link := range resultLinks {
url, ok := link.Attributes()[`href`]
// skip if href is not defined in the map
if !ok {
continue
}
// print the results in the following format: [Example Title](https://example.com)
fmt.Printf("Result: [%s](%s)\n", link.Text(), url)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment