Custom link extraction | crawler-go | Spatie       

 SPATIE  

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

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Crawler-go](https://spatie.be/docs/crawler-go/v1)  Advanced-usage  Custom link extraction

 Version   v1      

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

  Custom link extraction    
- [ 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)

 Custom link extraction
======================

###  On this page 

1. [ Crawling sitemaps ](#content-crawling-sitemaps)

You can customize link extraction by implementing `Parser`. `ExtractURLs` receives bounded body bytes and the final URL. It returns discoveries rather than modifying the queue directly:

```
type MyParser struct{}

func (MyParser) ExtractURLs(body []byte, baseURL string) ([]crawler.ExtractedURL, error) {
    return []crawler.ExtractedURL{{
        URL: "https://example.com/page",
        LinkText: "Example page",
        ResourceType: crawler.Link,
    }}, nil
}
```

Set `c.Parser = MyParser{}`. For inline parsing, use `ParserFunc`. Parsers run serially on the scheduler. A parser error stops `Start` and is returned to the caller. A malformed reference should instead set `MalformedReason` on that `ExtractedURL`, so other discoveries can still be used.

Each discovery has `URL`, `LinkText`, `ResourceType` and `MalformedReason`. Return absolute HTTP(S) URLs, or delegate relative resolution to `LinkParser`. The crawler adds parent URL and depth, then applies scope, robots, limits and queue deduplication. An empty resource type becomes `Link`.

The default `LinkParser` extracts anchors, next/previous links and hreflang elements. Configure its `ResourceTypes` to include assets. A custom parser replaces it entirely, so delegate to `LinkParser.ExtractURLs` when you also want built-in discovery.

Crawling sitemaps
-----------------------------------------------------------------------------------------------------------

```
c.Parser = crawler.SitemapParser{}
```

Start at a sitemap URL. `SitemapParser` accepts namespaced XML sitemap indexes and URL sets, extracting direct `loc` children of `sitemap` and `url` elements. Non-HTTP locations and duplicates are ignored. Invalid XML returns an error. It does not treat image/video locations as page URLs, discover sitemap files automatically or decompress `.gz` files beyond HTTP content-encoding support.

Child locations increase depth by one, just like PHP. A parser applies to every fetched response. If an XML crawl reaches HTML pages and should continue parsing both, supply a parser that selects `SitemapParser` for XML and `LinkParser` for HTML.

 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)
