我在做一些网刮使用colly但是想定期运行它的使用cron。 我没有尝试了一个基本方法。
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")
}
它似乎不能工作,使得一个叫一次,并且不定期的下一次呼叫。 不知道,如果我丢失了一些东西。 是否有任何其他办法,可以吗?
任何帮助,将不胜感激。
谢谢!
{1 {30s} 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC 0x6efa80 0x6efa80}]
. 是不是有帮助我。 这不会帮助吗?