Extracting resources | crawler-go | Spatie       

 SPATIE  

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

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Crawler-go](https://spatie.be/docs/crawler-go/v1)  Configuring-the-crawler  Extracting resources

 Version   v1      

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

  Extracting resources    
- [ 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)

 Extracting resources
====================

###  On this page 

1. [ Extracting specific resource types ](#content-extracting-specific-resource-types)
2. [ Extracting all resource types ](#content-extracting-all-resource-types)
3. [ Resource types in observers ](#content-resource-types-in-observers)
4. [ Base href support ](#content-base-href-support)
5. [ Malformed URLs ](#content-malformed-urls)

By default, the crawler only extracts links from each page. It can also extract images, scripts, stylesheets and Open Graph images. This is useful for broken asset checks or building an inventory of a site's resources.

Extracting specific resource types
--------------------------------------------------------------------------------------------------------------------------------------------------------------

```
c.Parser = crawler.LinkParser{
    ResourceTypes: []crawler.ResourceType{crawler.Image, crawler.Stylesheet},
}
```

Links are always included. The available types are:

TypeWhat it extracts`Link``<a href>`, `<link rel="next/prev">`, `<link hreflang>``Image``<img src>` and `<img data-src>``Script``<script src>` and `<link rel="modulepreload">``Stylesheet``<link rel="stylesheet">`, `<link type="text/css">`, `<link as="style">``OpenGraphImage``<meta property="og:image">` and `<meta property="twitter:image">`Extracting all resource types
-----------------------------------------------------------------------------------------------------------------------------------------------

```
c.Parser = crawler.LinkParser{ResourceTypes: []crawler.ResourceType{
    crawler.Image, crawler.Script, crawler.Stylesheet, crawler.OpenGraphImage,
}}
```

Resource types in observers
-----------------------------------------------------------------------------------------------------------------------------------------

Successes expose `r.ResourceType`. Failures expose `f.ResourceType`, `f.FoundOnURL`, `f.LinkText` and `f.Depth`. Broken images with 404 responses go through `OnFailed`, not `OnCrawled`. When collecting URLs, each `CrawledURL` also has its resource type.

Assets share the crawl's profile, robots checks and limits. External CDN assets may need an always-crawl rule or a wider profile. Deduplication is by URL, so the first resource type found wins. CSS imports, image srcset and arbitrary browser subresources are not discovered.

Base href support
-----------------------------------------------------------------------------------------------------------

Links and resources respect the first parsable `<base href>`. Relative URLs otherwise resolve against the final HTTP URL after redirects. Fragments are removed and dot segments are resolved.

Malformed URLs
--------------------------------------------------------------------------------------------------

Malformed HTTP references produce `Failure` notifications with the raw URL and a diagnostic error. They do not suppress the containing page or enter the queue. Within one document, references deduplicate by URL and resource type. Non-HTTP references are ignored. Link text is capped at 4000 UTF-8 bytes.

 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)
