You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: sources/academy/webscraping/advanced_web_scraping/crawling/crawling-sitemaps.md
+24-18Lines changed: 24 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,54 +9,59 @@ paths:
9
9
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.
10
10
11
11
We will look at the following topics:
12
+
12
13
- How to find sitemap URLs
13
14
- How to set up HTTP requests to download sitemaps
14
15
- How to parse URLs from sitemaps
15
16
- Using Crawlee to get all URLs in a few lines of code
16
17
17
18
## [](#how-to-find-sitemap-urls) How to find sitemap URLs
19
+
18
20
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.
19
21
20
22
### [](#google) Google
23
+
21
24
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.
22
25
23
26
### [](#robots-txt) robots.txt
27
+
24
28
If the website has a robots.txt file, it often contains sitemap URLs. The sitemap URLs are usually listed under `Sitemap:` directive.
25
29
26
30
### [](#common-url-paths) Common URL paths
31
+
27
32
You can try to iterate over common URL paths like:
28
-
```
29
-
/sitemap.xml
30
-
/product_index.xml
31
-
/product_template.xml
32
-
/sitemap_index.xml
33
-
/sitemaps/sitemap_index.xml
34
-
/sitemap/product_index.xml
35
-
/media/sitemap.xml
36
-
/media/sitemap/sitemap.xml
37
-
/media/sitemap/index.xml
38
-
```
33
+
34
+
- /sitemap.xml
35
+
- /product_index.xml
36
+
- /product_template.xml
37
+
- /sitemap_index.xml
38
+
- /sitemaps/sitemap_index.xml
39
+
- /sitemap/product_index.xml
40
+
- /media/sitemap.xml
41
+
- /media/sitemap/sitemap.xml
42
+
- /media/sitemap/index.xml
39
43
40
44
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`).
41
45
42
46
Some websites also provide an HTML version, to help indexing bots find new content. Those include:
43
47
44
-
```
45
-
/sitemap
46
-
/category-sitemap
47
-
/sitemap.html
48
-
/sitemap_index
49
-
```
48
+
- /sitemap
49
+
- /category-sitemap
50
+
- /sitemap.html
51
+
- /sitemap_index
50
52
51
53
Apify provides the [Sitemap Sniffer actor](https://apify.com/vaclavrut/sitemap-sniffer) (open-source code), that scans the URL variations automatically for you so that you don't have to check manually.
52
54
53
55
## [](#how-to-set-up-http-requests-to-download-sitemaps) How to set up HTTP requests to download sitemaps
56
+
54
57
For most sitemaps, you can make a simple HTTP request and parse the downloaded XML text with Cheerio (or just use `CheerioCrawler`). Some sitemaps are compressed and have to be streamed and decompressed. The code for that is fairly complicated so we recommend just [using Crawlee](#using-crawlee) which handles streamed and compressed sitemaps by default.
55
58
56
59
## [](#how-to-parse-urls-from-sitemaps) How to parse URLs from sitemaps
57
-
The easiest part is to parse the actual URLs from the sitemap. The URLs are usually listed under `<loc>` tags. You can use Cheerio to parse the XML text and extract the URLs. 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). [This article](academy/tutorials/node-js/scraping-from-sitemaps.md) provides code examples for parsing sitemaps.
60
+
61
+
The easiest part is to parse the actual URLs from the sitemap. The URLs are usually listed under `<loc>` tags. You can use Cheerio to parse the XML text and extract the URLs. 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). [This article](academy/node-js/scraping-from-sitemaps.md) provides code examples for parsing sitemaps.
58
62
59
63
## [](#using-crawlee) Using Crawlee
64
+
60
65
Fortunately, you don't have to worry about any of the above steps if you use [Crawlee](https://crawlee.dev) which has rich traversing and parsing support for sitemap. Crawlee can traverse nested sitemaps, download, and parse compressed sitemaps, and extract URLs from them. You can get all URLs in a few lines of code:
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](academy/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.
286
286
287
287
Check out the [full code example](https://github.com/apify-projects/apify-extra-library/tree/master/examples/crawler-with-filters).
Copy file name to clipboardExpand all lines: sources/academy/webscraping/advanced_web_scraping/crawling/sitemaps-vs-search.md
+7Lines changed: 7 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,35 +17,42 @@ There are two main approaches to solving this problem:
17
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
18
19
19
## Pros and cons of sitemaps
20
+
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.
21
22
22
23
### Pros
24
+
23
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.
24
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.
25
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.
26
28
27
29
### Cons
30
+
28
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.
29
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.
30
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.
31
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. Fortunately, we will get to this in the next lesson.
32
35
33
36
## Pros and cons of categories, search, and filters
37
+
34
38
This approach means traversing the website like a normal user do by going through categories, setting up different filters, ranges and sorting options. The goal is to traverse it is a way that ensures we covered all categories/ranges where products can be located and for each of those we stayed under the pagination limit.
35
39
36
40
The pros and cons of this approach are pretty much the opposite of the sitemaps approach.
37
41
38
42
### Pros
43
+
39
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.
40
45
-**Updated in real-time** - The website is updated in real-time so we can be sure that we are getting all pages.
41
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.
42
47
43
48
### Cons
49
+
44
50
-**Complex to set up** - The logic to traverse the website is usually more complex and can take a lot of time to get right. We will get to this in the next lessons.
45
51
-**Slow to run** - The traversing can require a lot of requests. Some filters or categories will have products we already found.
46
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 framework we will build in the next lessons will help us with this.
47
53
48
54
## Do we know how many products there are?
55
+
49
56
Fortunately, most websites list a total number of detail pages somewhere. It might be displayed on the home page or 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.
50
57
51
58
Unfortunately, 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 next lessons as well.
Copy file name to clipboardExpand all lines: sources/academy/webscraping/advanced_web_scraping/index.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,7 @@ paths:
9
9
10
10
# Advanced web scraping
11
11
12
-
In [**Web scraping for beginners**](/academy/webscraping/scraping_basics_javascript/index.md) 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.
12
+
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.
13
13
14
14
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.
0 commit comments