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.
TypeScript apifyautomationcrawlercrawlingheadlessheadless-chromejavascriptnodejsnpmplaywrightpuppeteerscraperscrapingtypescriptweb-crawlerweb-crawlingweb-scraping
CheerioCrawler: text/xml content type works (#826)
Jan Bárta0b45638b0ae7
diff --git a/src/crawlers/cheerio_crawler.js b/src/crawlers/cheerio_crawler.jsindex 18c7bbf91..6cd1236b1 100644--- a/src/crawlers/cheerio_crawler.js+++ b/src/crawlers/cheerio_crawler.js@@ -32,7 +32,8 @@ import { validators } from '../validators';/*** Default mime types, which CheerioScraper supports.*/-const DEFAULT_MIME_TYPES = ['text/html', 'application/xhtml+xml'];+const HTML_AND_XML_MIME_TYPES = ['text/html', 'text/xml', 'application/xhtml+xml', 'application/xml'];+const APPLICATION_JSON_MIME_TYPE = 'application/json';const DEFAULT_AUTOSCALED_POOL_OPTIONS = {snapshotterOptions: {eventLoopSnapshotIntervalSecs: 2,@@ -401,7 +402,7 @@ class CheerioCrawler {throw new Error('Cannot use "options.persistCookiesPerSession" without "options.useSessionPool"');}- this.supportedMimeTypes = new Set(DEFAULT_MIME_TYPES);+ this.supportedMimeTypes = new Set([...HTML_AND_XML_MIME_TYPES, APPLICATION_JSON_MIME_TYPE]);if (additionalMimeTypes.length) this._extendSupportedMimeTypes(additionalMimeTypes);if (requestOptions) {@@ -549,7 +550,7 @@ class CheerioCrawler {crawlingContext.response = response;Object.defineProperty(crawlingContext, 'json', {get() {- if (contentType.type !== 'application/json') return null;+ if (contentType.type !== APPLICATION_JSON_MIME_TYPE) return null;const jsonString = body.toString(contentType.encoding);return JSON.parse(jsonString);},@@ -628,7 +629,7 @@ class CheerioCrawler {// Errors are often sent as JSON, so attempt to parse them,// despite Accept header being set to text/html.- if (type === 'application/json') {+ if (type === APPLICATION_JSON_MIME_TYPE) {const errorResponse = JSON.parse(body);let { message } = errorResponse;if (!message) message = util.inspect(errorResponse, { depth: 1, maxArrayLength: 10 });@@ -637,7 +638,7 @@ class CheerioCrawler {// It's not a JSON so it's probably some text. Get the first 100 chars of it.throw new Error(`${statusCode} - Internal Server Error: ${body.substr(0, 100)}`);- } else if (type === 'text/html' || type === 'application/xhtml+xml' || type === 'application/xml') {+ } else if (HTML_AND_XML_MIME_TYPES.includes(type)) {const dom = await this._parseHtmlToDom(response);return ({ dom, isXml: type.includes('xml'), response, contentType });} else {diff --git a/test/crawlers/cheerio_crawler.test.js b/test/crawlers/cheerio_crawler.test.jsindex 2461f5a58..d46630cc6 100644--- a/test/crawlers/cheerio_crawler.test.js+++ b/test/crawlers/cheerio_crawler.test.js@@ -364,7 +364,7 @@ describe('CheerioCrawler', () => {crawler = new Apify.CheerioCrawler({requestList: await getRequestListForMock(port, {headers: {- 'content-type': 'application/json',+ 'content-type': 'text/plain',},statusCode: 200,}),@@ -382,8 +382,8 @@ describe('CheerioCrawler', () => {expect(handlePageInvocationCount).toBe(0);expect(errorMessages).toHaveLength(4);errorMessages.forEach((msg) => expect(msg).toMatch(- 'Content-Type application/json, but only text/html, '- + 'application/xhtml+xml are allowed. Skipping resource.',+ ' Content-Type text/plain, but only text/html, text/xml, application/xhtml+xml, application/xml, application/json are allowed.'+ + ' Skipping resource.',));});@@ -458,7 +458,7 @@ describe('CheerioCrawler', () => {test('should work with all defaults content types', async () => {let handledRequests = 0;- const contentTypes = ['text/html', 'application/xhtml+xml'];+ const contentTypes = ['text/html', 'application/xhtml+xml', 'text/xml', 'application/xml', 'application/json'];const sources = contentTypes.map((contentType) => ({url: `http://${HOST}:${port}/mock?ct=${contentType}`,payload: JSON.stringify({ headers: { 'Content-Type': contentType }, statusCode: 200 }),