-
-
Save weienwong/b8abe0c55f2159834d9f5ff27aa3816a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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