Skip to content

Instantly share code, notes, and snippets.

@weienwong
Forked from grantglidewell/scraper.go
Created December 3, 2018 04:34
Show Gist options
  • Select an option

  • Save weienwong/b8abe0c55f2159834d9f5ff27aa3816a to your computer and use it in GitHub Desktop.

Select an option

Save weienwong/b8abe0c55f2159834d9f5ff27aa3816a to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
"time"
)
func main() {
fmt.Println(scrape("URL", "searchTerm"))
}
func check(e error) {
if e != nil {
panic(e)
}
}
func scrape(u string, t string) string {
resp, err := http.Get(u)
check(err)
defer resp.Body.Close()
bdy, rderr := ioutil.ReadAll(resp.Body)
check(rderr)
ts := strconv.FormatInt(time.Now().UTC().UnixNano(), 10)
fln := "scrape" + ts + ".html"
werr := ioutil.WriteFile(fln, bdy, 0644)
check(werr)
dat, err := ioutil.ReadFile(fln)
check(err)
if strings.Contains(string(dat), t) {
return "Found " + t + " in " + u
}
return t + " was not found in " + u
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment