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.

Refactored puppeteer crawler (#895)

petrpatekcbf83c49727b
diff --git a/src/crawlers/puppeteer_crawler.js b/src/crawlers/puppeteer_crawler.js
index 34b38e94f..1ac5f8b7e 100644
--- a/src/crawlers/puppeteer_crawler.js
+++ b/src/crawlers/puppeteer_crawler.js
@@ -148,21 +148,21 @@ import applyStealthToBrowser from '../stealth/stealth';
* which downloads the pages using raw HTTP requests and is about 10x faster.
*
* The source URLs are represented using {@link Request} objects that are fed from
- * {@link RequestList} or {@link RequestQueue} instances provided by the {@link BasicCrawlerOptions.requestList}
- * or {@link BasicCrawlerOptions.requestQueue} constructor options, respectively.
+ * {@link RequestList} or {@link RequestQueue} instances provided by the {@link PuppeteerCrawlerOptions.requestList}
+ * or {@link PuppeteerCrawlerOptions.requestQueue} constructor options, respectively.
*
- * If both {@link BasicCrawlerOptions.requestList} and {@link BasicCrawlerOptions.requestQueue} are used,
+ * If both {@link PuppeteerCrawlerOptions.requestList} and {@link PuppeteerCrawlerOptions.requestQueue} are used,
* the instance first processes URLs from the {@link RequestList} and automatically enqueues all of them
* to {@link RequestQueue} before it starts their processing. This ensures that a single URL is not crawled multiple times.
*
* The crawler finishes when there are no more {@link Request} objects to crawl.
*
* `PuppeteerCrawler` opens a new Chrome page (i.e. tab) for each {@link Request} object to crawl
- * and then calls the function provided by user as the {@link BrowserCrawlerOptions.handlePageFunction} option.
+ * and then calls the function provided by user as the {@link PuppeteerCrawlerOptions.handlePageFunction} option.
*
* New pages are only opened when there is enough free CPU and memory available,
* using the functionality provided by the {@link AutoscaledPool} class.
- * All {@link AutoscaledPool} configuration options can be passed to the {@link BasicCrawlerOptions.autoscaledPoolOptions}
+ * All {@link AutoscaledPool} configuration options can be passed to the {@link PuppeteerCrawlerOptions.autoscaledPoolOptions}
* parameter of the `PuppeteerCrawler` constructor. For user convenience, the `minConcurrency` and `maxConcurrency`
* {@link AutoscaledPoolOptions} are available directly in the `PuppeteerCrawler` constructor.
*
@@ -237,8 +237,7 @@ class PuppeteerCrawler extends BrowserCrawler {
ow(options, 'PuppeteerCrawlerOptions', ow.object.exactShape(PuppeteerCrawler.optionsShape));
const {
- puppeteerModule, // eslint-disable-line
- launchContext = {},
+ launchContext = {}, // @TODO: should not launcher be inside launchContext
gotoTimeoutSecs,
navigationTimeoutSecs,
browserPoolOptions = {},
@@ -250,16 +249,17 @@ class PuppeteerCrawler extends BrowserCrawler {
stealth,
stealthOptions,
proxyUrl,
+ launcher,
} = launchContext;
if (proxyUrl && proxyConfiguration) {
throw new Error('It is not possible to combine "options.proxyConfiguration" together with '
- + 'custom "proxyUrl" option from "options.launchPuppeteerOptions".');
+ + 'custom "proxyUrl" option from "options.launchContext".');
}
browserPoolOptions.browserPlugins = [
new PuppeteerPlugin(
- getPuppeteerOrThrow(puppeteerModule),
+ getPuppeteerOrThrow(launcher),
{
proxyUrl,
launchOptions: apifyOptionsToLaunchOptions(launchContext),
@@ -286,7 +286,7 @@ class PuppeteerCrawler extends BrowserCrawler {
}
if (proxyUrl) {
- this.log.deprecated('options.launchPuppeteerOptions.proxyUrl is deprecated use the options.proxyConfiguration instead');
+ this.log.deprecated('options.launchContext.proxyUrl is deprecated use the options.proxyConfiguration instead');
}
if (stealth) {
@@ -299,7 +299,6 @@ class PuppeteerCrawler extends BrowserCrawler {
this.navigationTimeoutMillis = (navigationTimeoutSecs || gotoTimeoutSecs) * 1000;
this.launchContext = launchContext;
- this.puppeteerModule = puppeteerModule;
this.defaultGotoOptions = {
timeout: this.navigationTimeoutMillis,
diff --git a/src/puppeteer.js b/src/puppeteer.js
index 88ebcb0e9..6c39e46d0 100644
--- a/src/puppeteer.js
+++ b/src/puppeteer.js
@@ -179,7 +179,6 @@ export function apifyOptionsToLaunchOptions(launchContext) {
*/
export const launchPuppeteer = async (launchContext = {}) => {
ow(launchContext, ow.object.partialShape({
- args: ow.optional.array.ofType(ow.string),
proxyUrl: ow.optional.string.url,
launcher: ow.optional.any(ow.string, ow.object),
stealth: ow.optional.boolean,
diff --git a/test/crawlers/puppeteer_crawler.test.js b/test/crawlers/puppeteer_crawler.test.js
index 706a657a0..d9fc41b8a 100644
--- a/test/crawlers/puppeteer_crawler.test.js
+++ b/test/crawlers/puppeteer_crawler.test.js
@@ -152,7 +152,7 @@ describe('PuppeteerCrawler', () => {
});
} catch (e) {
expect(e.message).toEqual('It is not possible to combine "options.proxyConfiguration" together with '
- + 'custom "proxyUrl" option from "options.launchPuppeteerOptions".');
+ + 'custom "proxyUrl" option from "options.launchContext".');
}
expect.hasAssertions();
diff --git a/test/puppeteer.test.js b/test/puppeteer.test.js
index 37acec910..ca01ad40d 100644
--- a/test/puppeteer.test.js
+++ b/test/puppeteer.test.js
@@ -74,8 +74,8 @@ describe('Apify.launchPuppeteer()', () => {
expect(Apify.launchPuppeteer({ proxyUrl: 'socks5://user:[email protected]:1234' })).rejects.toThrow(Error);
expect(Apify.launchPuppeteer({ proxyUrl: ' something really bad' })).rejects.toThrow(Error);
- expect(Apify.launchPuppeteer({ args: 'wrong args' })).rejects.toThrow(Error);
- expect(Apify.launchPuppeteer({ args: [12, 34] })).rejects.toThrow(Error);
+ expect(Apify.launchPuppeteer({ launchOptions: { args: 'wrong args' } })).rejects.toThrow(Error);
+ expect(Apify.launchPuppeteer({ launchOptions: { args: [12, 34] } })).rejects.toThrow(Error);
});
test('opens supports non-HTTP proxies without authentication', async () => {