Skip to content

Instantly share code, notes, and snippets.

@turnipsoup
Created June 11, 2021 04:01
Show Gist options
  • Save turnipsoup/542777849e07bed71bd1ef201762b1cd to your computer and use it in GitHub Desktop.
Save turnipsoup/542777849e07bed71bd1ef201762b1cd to your computer and use it in GitHub Desktop.
Example of using goquery to fetch all `li` `href`s.
package main
import (
"fmt"
"log"
"net/http"
"github.com/PuerkitoBio/goquery"
)
// Main
func main() {
// HTTP get
response, err := http.Get("https://www.foodnetwork.com/recipes/recipes-a-z/v/p/1")
if err != nil {
log.Fatal(err)
}
// Close the HTTP request later, to be polite
defer response.Body.Close()
// Turn the raw HTTP response from the site into a goquery response object
document, err := goquery.NewDocumentFromReader(response.Body)
if err != nil {
log.Fatal("There was an error loading the HTTP body!", err)
}
// Find all of the links, and execute getLinkDetails
// li[class=m-PromoList__a-ListItem] finds all `li` with class `.class=m-PromoList__a-ListItem`
recipeUrls := make([]string, 0)
document.Find("li[class=m-PromoList__a-ListItem] a").Each(func(index int, element *goquery.Selection) {
returnStr := "" // Define return string for manipulation
// Extract the target element, href, and check if it exists
href, exists := element.Attr("href")
if exists {
log.Println("Got URL: ", href)
}
returnStr = href
if href[:2] == "//" {
returnStr = href[2:]
}
// Append the fetched HREF to our global slice.
recipeUrls = append(recipeUrls, returnStr)
})
fmt.Println(recipeUrls)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment