Colly is the crawler framework most Go teams reach for, and the reason is that it ships the parts you would otherwise hand-write: a request queue, callbacks for documents and elements, per-domain rate limits and a proxy switcher. This golang colly tutorial covers a working collector, exits and limits, then the things that only surface at volume.
If you only need to fetch a handful of URLs, net/http plus goquery is simpler and you should stay there. Colly earns its place when a crawl has breadth, depth and rules.
Key Facts
- Colly is a crawler framework, not an HTTP client. It brings the queue, callbacks, per-domain limits and a proxy switcher, which is what you would otherwise write yourself around net/http.
- Proxy rotation is built in: SetProxyFunc with the round-robin switcher rotates exits per request without touching your handlers.
- Limits are per domain, not global. colly.LimitRule with a DomainGlob and Parallelism is the single most important line in a polite crawler.
- Async changes the contract: with Async true you must call Wait, and your callbacks run concurrently, so shared state needs protection.
- Rotation and sessions fight each other. Round-robin per request breaks cookie-backed journeys; use one collector per journey when state matters.
What does a working collector look like?
Four moving parts: the collector, the callbacks, the limit rule and the proxy switcher.
c := colly.NewCollector(
colly.AllowedDomains("example.com"),
colly.MaxDepth(2),
colly.Async(true),
)
rp, err := proxy.RoundRobinProxySwitcher(
"http://USER:[email protected]:823",
)
if err != nil { log.Fatal(err) }
c.SetProxyFunc(rp)
c.Limit(&colly.LimitRule{
DomainGlob: "*example.com*",
Parallelism: 4,
RandomDelay: 2 * time.Second,
})
c.OnHTML("a[href]", func(e *colly.HTMLElement) {
e.Request.Visit(e.Attr("href"))
})
c.OnHTML("h1", func(e *colly.HTMLElement) {
fmt.Println(e.Text)
})
c.OnError(func(r *colly.Response, err error) {
log.Printf("%s -> %d %v", r.Request.URL, r.StatusCode, err)
})
c.Visit("https://example.com/")
c.Wait()
Two lines carry most of the behaviour. The LimitRule is what keeps the crawl polite, and RandomDelay is what stops your requests arriving on a metronome. The proxy switcher rotates exits per request, which is right for breadth-first crawling and wrong for anything with a session.
What is the collector lifecycle? The 4-step request path
Every request walks the same 4-step request path, and knowing it tells you where to put your code.
| Step | Hook | Use it for |
|---|---|---|
| 1. Queued | OnRequest | Headers, per-request context, skipping URLs with Abort |
| 2. Fetched | OnResponse | Raw body handling, saving HTML, content-type checks |
| 3. Parsed | OnHTML, OnXML | Extraction and following links |
| 4. Finished | OnScraped, OnError | Counters, persistence, retry decisions |
The common mistake is doing extraction in OnResponse and link-following in OnHTML, which splits one concern across two hooks. Keep raw handling in OnResponse and everything selector-based in OnHTML, and the crawler stays readable when someone else changes it.
How do you handle rotation, sessions and retries?
Decide first whether the crawl has state. Rotation and sessions pull in opposite directions.
No state. Breadth-first crawling of public pages is the case the round-robin switcher was built for: a fresh exit per request, no cookies that matter, maximum spread.
State. Search flows, pagination behind a session, anything cookie-backed. Use one collector per journey with a single sticky exit, and keep its cookie jar for the life of that journey. Sharing one collector across journeys while rotating exits is the contradiction that produces sudden challenge pages.
Retries. Colly has no retry policy of its own; OnError is where you add one. Retry 429, 503 and 504 with backoff and jitter, never retry 404 or an unchanged 403, and keep a per-host failure counter that stops the crawl rather than grinding through a ban. The full code map is in our reference on HTTP status codes for scrapers.
What bites at volume?
Four things, none of which appear in a hundred-page test run.
Async without Wait. With Async true the program exits before the queue drains unless you call Wait, and the symptom is a crawl that silently collects a fraction of what it should.
Callbacks race on shared state. Handlers run concurrently, so appending to a slice or writing to a map from OnHTML needs a mutex or a channel. Most mysterious data corruption in Colly crawls is this.
Memory growth on deep crawls. The visited-URL set grows with the crawl. For large jobs use a storage backend rather than the in-memory default, or the process quietly swells.
Limits that do not match reality. Parallelism is per matching domain glob. A glob that matches nothing means no limit at all, which is how a polite-looking config produces a burst.
When is Colly the wrong tool?
Use Colly when the job is a crawl: many URLs, link following, per-domain rules, and no JavaScript rendering. Avoid it when the target renders client-side, because Colly fetches HTML and does not drive a browser; chromedp or an external renderer is the answer there, and our Go section in the proxy setup reference covers the plain-client alternative.
Also avoid it for single-page fetches inside a larger service, where the framework’s lifecycle is overhead you do not need.
On the rules, unchanged by tooling: honour robots.txt, which Colly can enforce for you, respect the site terms, prefer official APIs, and keep away from anything behind a login. General information rather than legal advice; see is web scraping legal.
Frequently Asked Questions
How do I set a proxy in Colly?
Build a switcher with proxy.RoundRobinProxySwitcher passing one or more gateway URLs with credentials, then hand it to c.SetProxyFunc. That rotates the exit per request without touching your handlers, which is what you want for stateless crawling and what you must avoid for cookie-backed journeys.
Why does my Colly crawl stop early?
Almost always Async true without a matching c.Wait, so the program exits while the queue is still draining. The tell is a run that finishes suspiciously fast and collects a fraction of the expected rows. Add Wait, and assert on row counts so the next occurrence is an alert rather than a mystery.
How do I rate limit per domain?
Use c.Limit with a LimitRule that sets DomainGlob, Parallelism and RandomDelay. Parallelism caps concurrent requests to matching domains and RandomDelay adds jitter so requests do not arrive on a metronome. Check the glob actually matches your targets: a glob that matches nothing applies no limit.
Can Colly render JavaScript?
No. It fetches and parses HTML, so pages that build their content client-side come back empty. Either find the JSON endpoint the page calls, or drive a real browser with chromedp and accept the cost per page.
Should I use one collector or many?
One per journey when state matters, because the cookie jar and the exit belong together. One shared collector is fine for stateless breadth-first crawling, where the round-robin switcher is an advantage rather than a problem.
Give the switcher a pool worth rotating
Colly rotates whatever exits you give it. DataImpulse residential proxies provide geo-accurate exits at $1 per GB across 195 countries, rotating or sticky, over HTTP, HTTPS and SOCKS5, so the switcher above works with one credential. Create an account and test one domain before widening the crawl.
Related: proxy setup by language · status codes for scrapers · rotation best practices.
Last updated: September 17, 2026.
