Last active
September 17, 2021 14:14
-
-
Save sagar290/9336c028609b7191dfbd9a9d848de149 to your computer and use it in GitHub Desktop.
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 ( | |
"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