---
id: crawl-website-with-relative-links
title: Crawl website with relative links
---

import ApiLink from '@site/src/components/ApiLink';
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
import CodeBlock from '@theme/CodeBlock';

import AllLinksExample from '!!raw-loader!./code/crawl_website_with_relative_links_all_links.py';
import SameDomainExample from '!!raw-loader!./code/crawl_website_with_relative_links_same_domain.py';
import SameHostnameExample from '!!raw-loader!./code/crawl_website_with_relative_links_same_hostname.py';
import SameOriginExample from '!!raw-loader!./code/crawl_website_with_relative_links_same_origin.py';

When crawling a website, you may encounter various types of links that you wish to include in your crawl. To facilitate this, we provide the <ApiLink to="class/EnqueueLinksFunction">`enqueue_links`</ApiLink> method on the crawler context, which will automatically find and add these links to the crawler's <ApiLink to="class/RequestQueue">`RequestQueue`</ApiLink>. This method simplifies the process of handling different types of links, including relative links, by automatically resolving them based on the page's context.

:::note

For these examples, we are using the <ApiLink to="class/BeautifulSoupCrawler">`BeautifulSoupCrawler`</ApiLink>. However, the same method is available for other crawlers as well. You can use it in exactly the same way.

:::

<ApiLink to="enum/EnqueueStrategy">`EnqueueStrategy`</ApiLink> enum provides four distinct strategies for crawling relative links:

- `EnqueueStrategy.All` - Enqueues all links found, regardless of the domain they point to. This strategy is useful when you want to follow every link, including those that navigate to external websites.
- `EnqueueStrategy.SAME_DOMAIN` - Enqueues all links found that share the same domain name, including any possible subdomains. This strategy ensures that all links within the same top-level and base domain are included.
- `EnqueueStrategy.SAME_HOSTNAME` - Enqueues all links found for the exact same hostname. This is the **default** strategy, and it restricts the crawl to links that have the same hostname as the current page, excluding subdomains.
- `EnqueueStrategy.SAME_ORIGIN` - Enqueues all links found that share the same origin. The same origin refers to URLs that share the same protocol, domain, and port, ensuring a strict scope for the crawl.

<Tabs groupId="main">
    <TabItem value="all_links" label="All links">
        <CodeBlock className="language-python">
            {AllLinksExample}
        </CodeBlock>
    </TabItem>
    <TabItem value="same_domain" label="Same domain">
        <CodeBlock className="language-python">
            {SameDomainExample}
        </CodeBlock>
    </TabItem>
    <TabItem value="same_hostname" label="Same hostname">
        <CodeBlock className="language-python">
            {SameHostnameExample}
        </CodeBlock>
    </TabItem>
    <TabItem value="same_origin" label="Same origin">
        <CodeBlock className="language-python">
            {SameOriginExample}
        </CodeBlock>
    </TabItem>
</Tabs>
