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
added quality option and changed from png to jpeg
metalwarrior665edf3406a1658
diff --git a/src/puppeteer_utils.js b/src/puppeteer_utils.jsindex d5fdb121f..05cf4e977 100644--- a/src/puppeteer_utils.js+++ b/src/puppeteer_utils.js@@ -518,10 +518,11 @@ export const infiniteScroll = async (page, options = {}) => {* Puppeteer <a href="https://pptr.dev/#?product=Puppeteer&show=api-class-page" target="_blank"><code>Page</code></a> object.* @param {Object} [options]* @param {String} [options.key=SNAPSHOT]- * Key under which the screenshot and HTML will be saved. `.png` will be appended for screenshot and `.html` for HTML.- * Must contain only letters, numbers, dashes, dots and underscores.+ * Key under which the screenshot and HTML will be saved. `.jpg` will be appended for screenshot and `.html` for HTML.+ * @param {Number} [options.quality=50]+ * The quality of the image, between 0-100. Higher quality images have bigger size and require more storage.* @param {Boolean} [options.saveScreenshot=true]- * If true, it will save a full screenshot of the current page as a record with `key` appended by `.png`.+ * If true, it will save a full screenshot of the current page as a record with `key` appended by `.jpg`.* @param {Boolean} [options.saveHtml=true]* If true, it will save a full HTML of the current page as a record with `key` appended by `.html`.* @param {String} [options.storeName=null]@@ -537,19 +538,20 @@ const saveSnapshot = async (page, options = {}) => {checkParamOrThrow(page, 'page', 'Object');checkParamOrThrow(options, 'options', 'Object');- const { saveScreenshot = true, saveHtml = true, keyValueStoreName = null } = options;+ const { saveScreenshot = true, saveHtml = true, keyValueStoreName = null, quality = 50 } = options;key = options.key || DEFAULT_KEY;checkParamOrThrow(saveScreenshot, 'saveScreenshot', 'Boolean');checkParamOrThrow(saveHtml, 'saveHtml', 'Boolean');checkParamOrThrow(key, 'key', 'String');checkParamOrThrow(keyValueStoreName, 'keyValueStoreName', 'Maybe String');+ checkParamOrThrow(quality, 'quality', 'Number');const store = await openKeyValueStore(keyValueStoreName);if (saveScreenshot) {- const screenshotBuffer = await page.screenshot({ fullPage: true });- await store.setValue(`${key}.png`, screenshotBuffer, { contentType: 'image/png' });+ const screenshotBuffer = await page.screenshot({ fullPage: true, quality, type: 'jpeg' });+ await store.setValue(`${key}.jpg`, screenshotBuffer, { contentType: 'image/jpeg' });}if (saveHtml) {const html = await page.content();diff --git a/test/puppeteer_utils.js b/test/puppeteer_utils.jsindex 3adceec59..61a768719 100644--- a/test/puppeteer_utils.js+++ b/test/puppeteer_utils.js@@ -385,7 +385,7 @@ describe('Apify.utils.puppeteer', () => {const contentHTML = '<html><head></head><body><div style="border: 1px solid black">Div number: 1</div></body></html>';await page.setContent(contentHTML);- const screenshot = await page.screenshot({ fullPage: true });+ const screenshot = await page.screenshot({ fullPage: true, type: 'jpeg', quality: 60 });// Test saving both image and htmlconst object = { setValue: async () => {} };@@ -396,9 +396,9 @@ describe('Apify.utils.puppeteer', () => {.withExactArgs('TEST-STORE').resolves(object);- await Apify.utils.puppeteer.saveSnapshot(page, { key: 'TEST', keyValueStoreName: 'TEST-STORE' });+ await Apify.utils.puppeteer.saveSnapshot(page, { key: 'TEST', keyValueStoreName: 'TEST-STORE', quality: 60 });- expect(stub.calledWithExactly('TEST.png', screenshot, { contentType: 'image/png' })).to.be.eql(true);+ expect(stub.calledWithExactly('TEST.jpg', screenshot, { contentType: 'image/jpeg' })).to.be.eql(true);expect(stub.calledWithExactly('TEST.html', contentHTML, { contentType: 'text/html' })).to.be.eql(true);// Test saving only image@@ -410,7 +410,9 @@ describe('Apify.utils.puppeteer', () => {await Apify.utils.puppeteer.saveSnapshot(page, { saveHtml: false });- expect(stub2.calledOnceWithExactly('SNAPSHOT.png', screenshot, { contentType: 'image/png' })).to.be.eql(true);+ // Default quality is 50+ const screenshot2 = await page.screenshot({ fullPage: true, type: 'jpeg', quality: 50 });+ expect(stub2.calledOnceWithExactly('SNAPSHOT.jpg', screenshot2, { contentType: 'image/jpeg' })).to.be.eql(true);mock.verify();} finally {