Last active
May 24, 2022 06:40
-
-
Save goodylili/e98f4c656b361a179050cd9e5390970a to your computer and use it in GitHub Desktop.
scraping hackernews using the Goquery package
This file contains 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" | |
"github.com/PuerkitoBio/goquery" | |
"net/http" | |
) | |
type Information struct { | |
link string | |
title string | |
} | |
func main() { | |
info := make([]Information, 0) | |
webUrl := "https://news.ycombinator.com/" | |
response, _ := http.Get(webUrl) | |
// get status code (article should contain this) | |
document, _ := goquery.NewDocumentFromReader(response.Body) | |
document.Find("tr.athing").Each(func(index int, selector *goquery.Selection) { | |
title := selector.Find("td.title").Text() | |
getLink, _ := selector.Find("a.titlelink").Attr("href") | |
info = append(info, Information{ | |
title: title, | |
link: getLink, | |
}) | |
}) | |
fmt.Println(info) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment