运行Colly网刮周期性地使用计划中去

0

的问题

我在做一些网刮使用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")
}

它似乎不能工作,使得一个叫一次,并且不定期的下一次呼叫。 不知道,如果我丢失了一些东西。 是否有任何其他办法,可以吗?

任何帮助,将不胜感激。

谢谢!

cron go go-colly web-scraping
2021-11-13 06:06:49
1

最好的答案

0

c.AddFunc 返回 error 你是不是检查,请做的情况下,揭示了进一步的信息。

你应该能够检查返回的 c.Entries() 这应该给你的有关信息的下一次你的功能将被称为。

在情况下你都不知道,你不需要一个完整的图书馆完成执行功能的周期性。 例如,你可以做到:

scrap := newScraper(coll, rc)

sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)
ticker := time.NewTicker(10 * time.Second)

// Run the function initially, so we don't have to wait 10 seconds for the first run (optional).
scrapePls()
for {
    select {
    case <-ticker.C:
        // Ticker will send a message every 10 seconds
        scrapePls()

        // You can also start a go routine every time. If scrapePls takes more than the interval
        // to run this may lead to issues to due to an forever increasing number of goroutines.
        // go scrapePls()
        
    case <-sig
        return
    }
}
2021-11-13 11:57:51

谢谢你的解决方案约使用的股票用于定期称呼它。 我没有加入c。项并没有得到这个 {1 {30s} 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC 0x6efa80 0x6efa80}]. 是不是有帮助我。 这不会帮助吗?
Adith Dev Reddy

它还停止之后的第一个电话。
Adith Dev Reddy

什么样的 c.Entries 显示是它是预定的,只是为每30秒钟,并不是每一个10. 的时候仍然未初始化,他们我们被设定之后的第一个执行。 作为"它仍然停止之后的第一个呼叫"-你的意思是有的股票? 如果是这样,这意味着你永远不会回来 scrapePls. 我建议你设置 深入研究 和步骤,通过你的节目所以你可以看到的事情是错误的
caveman

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................