Skip to content

Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.

feat(sitemap): Support CDATA in sitemaps (#2424)

Pavel 'Strajk' Dolecek635f046b7933
diff --git a/packages/utils/src/internals/sitemap.ts b/packages/utils/src/internals/sitemap.ts
index 973d25ff0..bf3c88e3d 100644
--- a/packages/utils/src/internals/sitemap.ts
+++ b/packages/utils/src/internals/sitemap.ts
@@ -95,7 +95,7 @@ export class Sitemap {
}
});
- parser.on('text', (text) => {
+ const onText = (text: string) => {
if (parsingState.loc) {
if (parsingState.context === 'sitemapindex') {
if (!parsingState.visitedSitemapUrls.includes(text)) {
@@ -106,7 +106,9 @@ export class Sitemap {
parsingState.urls.push(text);
}
}
- });
+ };
+ parser.on('text', onText);
+ parser.on('cdata', onText);
parser.on('end', onEnd);
parser.on('error', onError);
diff --git a/packages/utils/test/sitemap.test.ts b/packages/utils/test/sitemap.test.ts
index a766fec6c..b3791c52c 100644
--- a/packages/utils/test/sitemap.test.ts
+++ b/packages/utils/test/sitemap.test.ts
@@ -70,6 +70,15 @@ describe('Sitemap', () => {
'<A HREF="https://ads.google.com/home/">here</A>.',
'</BODY></HTML>',
].join('\n'))
+ .get('/sitemap_cdata.xml')
+ .reply(200, [
+ '<?xml version="1.0" encoding="UTF-8"?>',
+ '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',
+ '<url>',
+ '<loc><![CDATA[http://not-exists.com/catalog]]></loc>',
+ '</url>',
+ '</urlset>',
+ ].join('\n'))
.get('/sitemap.xml')
.reply(200, [
'<?xml version="1.0" encoding="UTF-8"?>',
@@ -143,6 +152,13 @@ describe('Sitemap', () => {
expect(sitemap.urls).toEqual([]);
});
+ it('handles CDATA in loc tags', async () => {
+ const sitemap = await Sitemap.load('http://not-exists.com/sitemap_cdata.xml');
+ expect(new Set(sitemap.urls)).toEqual(new Set([
+ 'http://not-exists.com/catalog',
+ ]));
+ });
+
it('autodetects sitemaps', async () => {
const sitemap = await Sitemap.tryCommonNames('http://not-exists.com/arbitrary_url?search=xyz');
expect(new Set(sitemap.urls)).toEqual(new Set([