I was doing some web scraping using colly but wanted to run it periodically using cron. I did try out a basic approach to it.
type scraper struct {
coll *colly.Collector
rc *redis.Client
}
func newScraper(c *colly.Collector, rc *redis.Client) scraper {
return scraper{coll: c, rc: rc}
}
func main() {
rc := redis.NewClient(&redis.Options{
Addr: "localhost:3000",
Password: "", // no password set
DB: 0, // use default DB
})
coll := colly.NewCollector()
scrape := newScraper(coll, rc)
c := cron.New()
c.AddFunc("@every 10s", scrape.scrapePls)
c.Start()
sig := make(chan int)
<-sig
}
func (sc scraper) scrapePls() {
sc.coll.OnHTML(`body`, func(e *colly.HTMLElement) {
//Extracting required content
//Using Redis to store data
})
sc.coll.OnRequest(func(r *colly.Request) {
log.Println("Visting", r.URL)
})
sc.coll.Visit("www.example.com")
}
It seems to not be working, makes a call once and doesn't periodically make the next call. Not sure if I am missing out on something. Is there any other approaches that can be taken?
Any help would be appreciated.
Thanks!