Skip to content

Instantly share code, notes, and snippets.

@sagar290
Last active September 17, 2021 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sagar290/9336c028609b7191dfbd9a9d848de149 to your computer and use it in GitHub Desktop.
Save sagar290/9336c028609b7191dfbd9a9d848de149 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"github.com/gocolly/colly"
)
type Fact struct {
Href string `json:"href"`
Title string `json:"description"`
}
func main() {
allfacts := make([]Fact, 0)
collector := colly.NewCollector()
collector.OnHTML(".story-link", func(element *colly.HTMLElement) {
href := element.Attr("href")
// factDesc := element.ChildAttr("h2", "home-title").
title := element.ChildText(".home-title")
fact := Fact{
Href: href,
Title: title,
}
allfacts = append(allfacts, fact)
})
collector.OnRequest(func(r *colly.Request) {
fmt.Println("Visiting", r.URL.String())
})
collector.Visit("https://thehackernews.com/")
writeJson(allfacts)
}
func writeJson(data []Fact) {
file, err := json.MarshalIndent(data, "", " ")
if err != nil {
log.Println("Unable to create Json")
return
}
_ = ioutil.WriteFile("news.json", file, 0644)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment