Skip to content

Instantly share code, notes, and snippets.

@grantglidewell
Last active December 4, 2018 18:02
Show Gist options
  • Select an option

  • Save grantglidewell/3785fa997098a75fda81483374e0df99 to your computer and use it in GitHub Desktop.

Select an option

Save grantglidewell/3785fa997098a75fda81483374e0df99 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 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
}
@grantglidewell
Copy link
Author

Thanks, very helpful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment