Crawl responses | crawler-go | Spatie       

 SPATIE  

crawler-go
==========

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Crawler-go](https://spatie.be/docs/crawler-go/v1)  Basic-usage  Crawl responses

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/crawler-go/v1) 

  Crawl responses    
- [ Introduction ](https://spatie.be/docs/crawler-go/v1/introduction)
- [ Installation &amp; setup ](https://spatie.be/docs/crawler-go/v1/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/crawler-go/v1/questions-issues)
- [ About us ](https://spatie.be/docs/crawler-go/v1/about-us)
- [ Releasing ](https://spatie.be/docs/crawler-go/v1/releasing)
- [ Changelog ](https://spatie.be/docs/crawler-go/v1/changelog)
- [ Support us ](https://spatie.be/docs/crawler-go/v1/support-us)

Basic usage
-----------

- [ Your first crawl ](https://spatie.be/docs/crawler-go/v1/basic-usage/starting-your-first-crawl)
- [ Crawl responses ](https://spatie.be/docs/crawler-go/v1/basic-usage/handling-crawl-responses)
- [ Using observers ](https://spatie.be/docs/crawler-go/v1/basic-usage/using-observers)
- [ Collecting URLs ](https://spatie.be/docs/crawler-go/v1/basic-usage/collecting-urls)
- [ Filtering URLs ](https://spatie.be/docs/crawler-go/v1/basic-usage/filtering-urls)
- [ Testing ](https://spatie.be/docs/crawler-go/v1/basic-usage/testing)
- [ Tracking progress ](https://spatie.be/docs/crawler-go/v1/basic-usage/tracking-progress)

Configuring the crawler
-----------------------

- [ Concurrency &amp; throttling ](https://spatie.be/docs/crawler-go/v1/configuring-the-crawler/crawl-behavior)
- [ Limits ](https://spatie.be/docs/crawler-go/v1/configuring-the-crawler/setting-crawl-limits)
- [ Extracting resources ](https://spatie.be/docs/crawler-go/v1/configuring-the-crawler/extracting-resources)
- [ Configuring requests ](https://spatie.be/docs/crawler-go/v1/configuring-the-crawler/configuring-requests)
- [ Response filtering ](https://spatie.be/docs/crawler-go/v1/configuring-the-crawler/handling-responses)
- [ Respecting robots.txt ](https://spatie.be/docs/crawler-go/v1/configuring-the-crawler/respecting-robots-txt)

Advanced usage
--------------

- [ JavaScript rendering ](https://spatie.be/docs/crawler-go/v1/advanced-usage/rendering-javascript)
- [ Custom link extraction ](https://spatie.be/docs/crawler-go/v1/advanced-usage/extracting-custom-links)
- [ Custom request handlers ](https://spatie.be/docs/crawler-go/v1/advanced-usage/custom-request-handlers)
- [ Crawling across requests ](https://spatie.be/docs/crawler-go/v1/advanced-usage/crawling-across-requests)
- [ Custom crawl queue ](https://spatie.be/docs/crawler-go/v1/advanced-usage/using-a-custom-crawl-queue)
- [ Graceful shutdown ](https://spatie.be/docs/crawler-go/v1/advanced-usage/graceful-shutdown)

 Crawl responses
===============

###  On this page 

1. [ Transfer statistics ](#content-transfer-statistics)
2. [ Redirect history ](#content-redirect-history)
3. [ Using the DOM crawler ](#content-using-the-dom-crawler)

When a URL is successfully crawled, your callback or observer receives a `Response`. Its body is retained as a bounded byte slice, and its headers use Go's `http.Header`:

```
fmt.Println(r.URL, r.Status, string(r.Body))
fmt.Println(r.Header.Get("Content-Type"))
fmt.Println(r.FoundOnURL, r.LinkText, r.Depth, r.ResourceType)
fmt.Println(r.IsSuccessful(), r.IsRedirect(), r.WasRedirected())
fmt.Println(r.FinalURL, r.RedirectHistory)
```

`CrawlURL` preserves the first discovery's URL, parent, link text, depth and resource type. The response has no live HTTP body to close. It remains usable after the callback returns. Treat it as read-only in observers so later observers see the same data.

Transfer statistics
-----------------------------------------------------------------------------------------------------------------

`r.Statistics` reports total transfer `Duration`, `DownloadedBytes` and `RemoteAddr`. `DNSLookup`, `Connect`, `TLSHandshake` and `TimeToFirstByte` are pointers to measured `time.Duration` values. They are nil when unavailable, for example on reused connections or faked responses. `Duration` includes redirect hops and bounded body reads, not retry waits or JavaScript rendering. Downloaded bytes count retained decompressed bytes, not wire traffic.

Exact request-wire size and separate redirect duration are not available. The Go port does not invent values for them.

Redirect history
--------------------------------------------------------------------------------------------------------

`RedirectHistory` lists followed destinations, including the final URL but not the original. Relative links resolve against `FinalURL`. The default client follows up to five redirects. Customize `Config.Client.CheckRedirect` to change this or return `http.ErrUseLastResponse` to expose 3xx responses without following them.

Using the DOM crawler
-----------------------------------------------------------------------------------------------------------------------

`DOM()` returns a [goquery](https://github.com/PuerkitoBio/goquery) document for CSS queries:

```
doc, err := r.DOM()
if err != nil {
    return err
}
title := doc.Find("title").First().Text()
fmt.Println(title)
```

Parsing is on demand. Keep the returned document if you want to query it repeatedly. Body bytes are not charset-transcoded by the crawler.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
