Skip to content

Commit 3b4c7cd

Browse files
metalwarrior665honzajavorekTC-MO
authored
feat(academy): add advanced crawling section with sitemaps and search (#1217)
Co-authored-by: Honza Javorek <[email protected]> Co-authored-by: Michał Olender <[email protected]>
1 parent fe51c7c commit 3b4c7cd

File tree

10 files changed

+180
-20
lines changed

10 files changed

+180
-20
lines changed

nginx.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,9 @@ server {
302302
rewrite ^/platform/actors/development/actor-definition/output-schema$ /platform/actors/development/actor-definition/dataset-schema permanent;
303303
rewrite ^academy/deploying-your-code/output-schema$ /academy/deploying-your-code/dataset-schema permanent;
304304

305+
# Academy restructuring
306+
rewrite ^academy/advanced-web-scraping/scraping-paginated-sites$ /academy/advanced-web-scraping/crawling/crawling-with-search permanent;
307+
305308
# Removed pages
306309
# GPT plugins were discontinued April 9th, 2024 - https://help.openai.com/en/articles/8988022-winding-down-the-chatgpt-plugins-beta
307310
rewrite ^/platform/integrations/chatgpt-plugin$ https://blog.apify.com/add-custom-actions-to-your-gpts/ redirect;

sources/academy/tutorials/node_js/scraping_from_sitemaps.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@ import Example from '!!raw-loader!roa-loader!./scraping_from_sitemaps.js';
99

1010
# How to scrape from sitemaps {#scraping-with-sitemaps}
1111

12+
:::tip Processing sitemaps automatically with Crawlee
13+
14+
Crawlee allows you to scrape sitemaps with ease. If you are using Crawlee, you can skip the following steps and just gather all the URLs from the sitemap in a few lines of code.
15+
16+
:::
17+
18+
```js
19+
import { RobotsFile } from 'crawlee';
20+
21+
const robots = await RobotsFile.find('https://www.mysite.com');
22+
23+
const allWebsiteUrls = await robots.parseUrlsFromSitemaps();
24+
```
25+
1226
**The sitemap.xml file is a jackpot for every web scraper developer. Take advantage of this and learn an easier way to extract data from websites using Crawlee.**
1327

1428
---
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
---
2+
title: Crawling sitemaps
3+
description: Learn how to extract all of a website's listings even if they limit the number of results pages. See code examples for setting up your scraper.
4+
sidebar_position: 2
5+
slug: /advanced-web-scraping/crawling/crawling-sitemaps
6+
---
7+
8+
In the previous lesson, we learned what is the utility (and dangers) of crawling sitemaps. In this lesson, we will go in-depth to how to crawl sitemaps.
9+
10+
We will look at the following topics:
11+
12+
- How to find sitemap URLs
13+
- How to set up HTTP requests to download sitemaps
14+
- How to parse URLs from sitemaps
15+
- Using Crawlee to get all URLs in a few lines of code
16+
17+
## How to find sitemap URLs
18+
19+
Sitemaps are commonly restricted to contain a maximum of 50k URLs so usually, there will be a whole list of them. There can be a master sitemap containing URLs of all other sitemaps or the sitemaps might simply be indexed in `robots.txt` and/or have auto-incremented URLs like `/sitemap1.xml`, `/sitemap2.xml`, etc.
20+
21+
### Google
22+
23+
You can try your luck on Google by searching for `site:example.com sitemap.xml` or `site:example.com sitemap.xml.gz` and see if you get any results. If you do, you can try to download the sitemap and see if it contains any useful URLs. The success of this approach depends on the website telling Google to index the sitemap file itself which is rather uncommon.
24+
25+
### robots.txt {#robots-txt}
26+
27+
If the website has a `robots.txt` file, it often contains sitemap URLs. The sitemap URLs are usually listed under `Sitemap:` directive.
28+
29+
### Common URL paths
30+
31+
You can check some common URL paths, such as the following:
32+
33+
/sitemap.xml
34+
/product_index.xml
35+
/product_template.xml
36+
/sitemap_index.xml
37+
/sitemaps/sitemap_index.xml
38+
/sitemap/product_index.xml
39+
/media/sitemap.xml
40+
/media/sitemap/sitemap.xml
41+
/media/sitemap/index.xml
42+
43+
Make also sure you test the list with `.gz`, `.tar.gz` and `.tgz` extensions and by capitalizing the words (e.g. `/Sitemap_index.xml.tar.gz`).
44+
45+
Some websites also provide an HTML version, to help indexing bots find new content. Those include:
46+
47+
/sitemap
48+
/category-sitemap
49+
/sitemap.html
50+
/sitemap_index
51+
52+
Apify provides the [Sitemap Sniffer](https://apify.com/vaclavrut/sitemap-sniffer), an open source actor that scans the URL variations automatically for you so that you don't have to check them manually.
53+
54+
## How to set up HTTP requests to download sitemaps
55+
56+
For most sitemaps, you can make a single HTTP request and parse the downloaded XML text. Some sitemaps are compressed and have to be streamed and decompressed. The code can get fairly complicated, but scraping frameworks, such as [Crawlee](#using-crawlee), can do this out of the box.
57+
58+
## How to parse URLs from sitemaps
59+
60+
Use your favorite XML parser to extract the URLs from inside the `<loc>` tags. Just be careful that the sitemap might contain other URLs that you don't want to crawl (e.g. `/about`, `/contact`, or various special category sections). For specific code examples, see [our Node.js guide](/academy/node-js/scraping-from-sitemaps).
61+
62+
## Using Crawlee
63+
64+
Fortunately, you don't have to worry about any of the above steps if you use [Crawlee](https://crawlee.dev), a scraping framework, which has rich traversing and parsing support for sitemap. It can traverse nested sitemaps, download, and parse compressed sitemaps, and extract URLs from them. You can get all the URLs in a few lines of code:
65+
66+
```js
67+
import { RobotsFile } from 'crawlee';
68+
69+
const robots = await RobotsFile.find('https://www.mysite.com');
70+
71+
const allWebsiteUrls = await robots.parseUrlsFromSitemaps();
72+
```
73+
74+
## Next up
75+
76+
That's all we need to know about sitemaps for now. Let's dive into a much more interesting topic - search, filters, and pagination.

sources/academy/webscraping/advanced_web_scraping/scraping_paginated_sites.md renamed to sources/academy/webscraping/advanced_web_scraping/crawling/crawling-with-search.md

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
---
2-
title: Overcoming pagination limits
2+
title: Crawling with search
33
description: Learn how to extract all of a website's listings even if they limit the number of results pages. See code examples for setting up your scraper.
4-
sidebar_position: 1
5-
slug: /advanced-web-scraping/scraping-paginated-sites
4+
sidebar_position: 3
5+
slug: /advanced-web-scraping/crawling/crawling-with-search
66
---
77

8-
# Scraping websites with limited pagination
8+
# Scraping websites with search
99

10-
**Learn how to extract all of a website's listings even if they limit the number of results pages. See code examples for setting up your scraper.**
10+
In this lesson, we will start with a simpler example of scraping HTML based websites with limited pagination.
1111

12-
---
13-
14-
Limited pagination is a common practice on e-commerce sites and is becoming more popular over time. It makes sense: a real user will never want to look through more than 200 pages of results, only bots love unlimited pagination. Fortunately, there are ways to overcome this limit while keeping our code clean and generic.
12+
Limiting pagination is a common practice on e-commerce sites. It makes sense: a real user will never want to look through more than 200 pages of results – only bots love unlimited pagination. Fortunately, there are ways to overcome this limit while keeping our code clean and generic.
1513

1614
![Pagination in on Google search results page](./images/pagination.png)
1715

@@ -283,7 +281,6 @@ await crawler.addRequests(requestsToEnqueue);
283281

284282
## Summary {#summary}
285283

286-
And that's it. We have an elegant solution to a complicated problem. In a real project, you would want to make this a bit more robust and [save analytics data](../../platform/expert_scraping_with_apify/saving_useful_stats.md). This will let you know what filters you went through and how many products each of them had.
284+
And that's it. We have an elegant solution for a complicated problem. In a real project, you would want to make this a bit more robust and [save analytics data](../../../platform/expert_scraping_with_apify/saving_useful_stats.md). This will let you know what filters you went through and how many products each of them had.
287285

288286
Check out the [full code example](https://github.com/apify-projects/apify-extra-library/tree/master/examples/crawler-with-filters).
289-
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
title: Sitemaps vs search
3+
description: Learn how to extract all of a website's listings even if they limit the number of results pages.
4+
sidebar_position: 1
5+
slug: /advanced-web-scraping/crawling/sitemaps-vs-search
6+
---
7+
8+
The core crawling problem comes to down to ensuring that we reliably find all detail pages on the target website or inside its categories. This is trivial for small sites. We just open the home page or category pages and paginate to the end as we did in the [Web Scraping for Beginners course](/academy/web-scraping-for-beginners).
9+
10+
Unfortunately, _most modern websites restrict pagination_ only to somewhere between 1 and 10,000 products. Solving this problem might seem relatively straightforward at first but there are multiple hurdles that we will explore in this lesson.
11+
12+
There are two main approaches to solving this problem:
13+
14+
- Extracting all page URLs from the website's _sitemap_.
15+
- Using **categories, search and filters** to split the website so we get under the pagination limit.
16+
17+
Both of these approaches have their pros and cons so the best solution is to _use both and combine the results_. Here we will learn why.
18+
19+
## Pros and cons of sitemaps
20+
21+
Sitemap is usually a simple XML file that contains a list of all pages on the website. They are created and maintained mainly for search engines like Google to help ensure that the website gets fully indexed there. They are commonly located at URLs like `https://example.com/sitemap.xml` or `https://example.com/sitemap.xml.gz`. We will get to work with sitemaps in the next lesson.
22+
23+
### Pros
24+
25+
- _Quick to set up_ - The logic to find all sitemaps and extract all URLs is usually simple and can be done in a few lines of code.
26+
- _Fast to run_ - You only need to run a single request for each sitemap that contains up to 50,000 URLs. This means you can get all the URLs in a matter of seconds.
27+
- _Usually complete_ - Websites have an incentive to keep their sitemaps up to date as they are used by search engines. This means that they usually contain all pages on the website.
28+
29+
### Cons
30+
31+
- _Does not directly reflect the website_ - There is no way you can ensure that all pages on the website are in the sitemap. The sitemap also can contain pages that were already removed and will return 404s. This is a major downside of sitemaps which prevents us from using them as the only source of URLs.
32+
- _Updated in intervals_ - Sitemaps are usually not updated in real-time. This means that you might miss some pages if you scrape them too soon after they were added to the website. Common update intervals are 1 day or 1 week.
33+
- _Hard to find or unavailable_ - Sitemaps are not always trivial to locate. They can be deployed on a CDN with unpredictable URLs. Sometimes they are not available at all.
34+
- _Streamed, compressed, and archived_ - Sitemaps are often streamed and archived with .tgz extensions and compressed with gzip. This means that you cannot use default HTTP client settings and must handle these cases with extra code or use a scraping framework.
35+
36+
## Pros and cons of categories, search, and filters
37+
38+
This approach means traversing the website like a normal user does by going through categories, setting up different filters, ranges, and sorting options. The goal is to ensure that we cover all categories or ranges where products can be located, and that for each of those we stay under the pagination limit.
39+
40+
The pros and cons of this approach are pretty much the opposite of relying on sitemaps.
41+
42+
### Pros
43+
44+
- _Directly reflects the website_ - With most scraping use-cases, we want to analyze the website as the regular users see it. By going through the intended user flow, we ensure that we are getting the same pages as the users.
45+
- _Updated in real-time_ - The website is updated in real-time so we can be sure that we are getting all pages.
46+
- _Often contain detailed data_ - While sitemaps are usually just a list of URLs, categories, searches and filters often contain additional data like product names, prices, categories, etc, especially if available via JSON API. This means that we can sometimes get all the data we need without going to the detail pages.
47+
48+
### Cons
49+
50+
- _Complex to set up_ - The logic to traverse the website is usually complex and can take a lot of time to get right. We will get to this in the next lessons.
51+
- _Slow to run_ - The traversing can require a lot of requests. Some filters or categories will have products we already found.
52+
- _Not always complete_ - Sometimes the combination of filters and categories will not allow us to ensure we have all products. This is especially painful for sites where we don't know the exact number of products we are looking for. The tools we'll build in the following lessons will help us with this.
53+
54+
## Do we know how many products there are?
55+
56+
Most websites list a total number of detail pages somewhere. It might be displayed on the home page, search results, or be provided in the API response. We just need to make sure that this number really represents the whole site or category we are looking to scrape. By knowing the total number of products, we can tell if our approach to scrape all succeeded or if we still need to refine it.
57+
58+
Some sites, like Amazon, do not provide exact numbers. In this case, we have to work with what they give us and put even more effort into making our scraping logic accurate. We will tackle this in the following lessons as well.
59+
60+
## Next up
61+
62+
Next, we will look into [sitemap crawling](./crawling-sitemaps.md). After that we will go through all the intricacies of the category, search and filter crawling, and build up tools implementing a generic approach that we can use on any website. At last, we will combine the results of both and set up monitoring and persistence to ensure we can run this regularly without any manual controls.
Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
---
22
title: Advanced web scraping
3-
description: Take your scrapers to the next level by learning various advanced concepts and techniques that will help you build highly scalable and reliable crawlers.
3+
description: Take your scrapers to a production-ready level by learning various advanced concepts and techniques that will help you build highly scalable and reliable crawlers.
44
sidebar_position: 6
55
category: web scraping & automation
66
slug: /advanced-web-scraping
77
---
88

9-
# Advanced web scraping
9+
In [Web scraping for beginners](/academy/web-scraping-for-beginners) course, we have learned the necessary basics required to create a scraper. In the following courses, we learned more about specific practices and techniques that will help us to solve most of the problems we will face.
1010

11-
**Take your scrapers to the next level by learning various advanced concepts and techniques that will help you build highly scalable and reliable crawlers.**
11+
In this course, we will take all of that knowledge, add a few more advanced concepts, and apply them to learn how to build a production-ready web scraper.
1212

13-
---
13+
## What does production-ready mean
14+
15+
To scrape large and complex websites, we need to scale two essential aspects of the scraper: crawling and data extraction. Big websites can have millions of pages and the data we want to extract requires more sophisticated parsing techniques than just selecting elements by CSS selectors or using APIs as they are.
16+
17+
<!-- WIP: We want to split this into crawling and data extraction
18+
The following sections will cover the core concepts that will ensure that your scraper is production-ready:
19+
The advanced crawling section will cover how to ensure we find all pages or products on the website.
20+
- The advanced data extraction will cover how to efficiently extract data from a particular page or API.
21+
-->
1422

15-
In this course, we'll be tackling some of the most challenging and advanced web-scraping cases, such as mobile-app scraping, scraping sites with limited pagination, and handling large-scale cases where millions of items are scraped. Are **you** ready to take your scrapers to the next level?
23+
We will also touch on monitoring, performance, anti-scraping protections, and debugging.
1624

1725
If you've managed to follow along with all of the courses prior to this one, then you're more than ready to take these upcoming lessons on 😎
1826

19-
## First up {#first-up}
27+
## First up
2028

21-
This course's [first lesson](./scraping_paginated_sites.md) dives head-first into one of the most valuable skills you can have as a scraper developer: **Scraping paginated sites**.
29+
First, we will explore [advanced crawling section](./crawling/sitemaps-vs-search.md) that will help us to find all pages or products on the website.

sources/academy/webscraping/api_scraping/general_api_scraping/handling_pagination.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,9 @@ Here's what the output of this code looks like:
196196
105
197197
```
198198
199-
## Final note {#final-note}
199+
## Final note
200200
201-
Sometimes, APIs have limited pagination. That means that they limit the total number of results that can appear for a set of pages, or that they limit the pages to a certain number. To learn how to handle these cases, take a look at [this short article](/academy/advanced-web-scraping/scraping-paginated-sites).
201+
Sometimes, APIs have limited pagination. That means that they limit the total number of results that can appear for a set of pages, or that they limit the pages to a certain number. To learn how to handle these cases, take a look at the [Crawling with search](/academy/advanced-web-scraping/crawling/crawling-with-search) article.
202202
203203
## Next up {#next}
204204

sources/academy/webscraping/puppeteer_playwright/common_use_cases/paginating_through_results.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import TabItem from '@theme/TabItem';
1616

1717
If you're trying to [collect data](../executing_scripts/extracting_data.md) on a website that has millions, thousands, or even hundreds of results, it is very likely that they are paginating their results to reduce strain on their back-end as well as on the users loading and rendering the content.
1818

19-
![Amazon pagination](../../advanced_web_scraping/images/pagination.png)
19+
![Amazon pagination](../../advanced_web_scraping/crawling/images/pagination.png)
2020

2121
## Page number-based pagination {#page-number-based-pagination}
2222

0 commit comments

Comments
 (0)