Last active
December 4, 2018 18:02
-
-
Save grantglidewell/3785fa997098a75fda81483374e0df99 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 scrape(u string, t string) (string, error) { | |
| // Make the http request for the resource | |
| resp, err := http.Get(u) | |
| if err != nil { | |
| return "", err | |
| } | |
| // Close the connection when the response is complete | |
| defer resp.Body.Close() | |
| // Read the response body to a variable | |
| body, err := ioutil.ReadAll(resp.Body) | |
| if err != nil { | |
| return "", err | |
| } | |
| // Create a unique token for filename | |
| ts := strconv.FormatInt(time.Now().UTC().UnixNano(), 10) | |
| // Generate a string for the filename | |
| filename := fmt.Sprintf("scrape%v.html", ts) | |
| // Write body as a file to disk | |
| ioutil.WriteFile(filename, body, 0644) | |
| // Read the file to a variable | |
| dat, err := ioutil.ReadFile(filename) | |
| if err != nil { | |
| return "", err | |
| } | |
| // Check if the search term is present | |
| if strings.Contains(string(dat), t) { | |
| // Return a message to the user that the search term was found | |
| return fmt.Sprintf("Found %[1]s in %[2]s", t, u), nil | |
| } | |
| // Return a message to the user that the search term was not found | |
| return fmt.Sprintf("%[1]s was not found in %[2]s", t, u), nil | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, very helpful