PuppeteerCrawlingContext <UserData>
Hierarchy
- BrowserCrawlingContext<PuppeteerCrawler, Page, HTTPResponse, PuppeteerController, UserData>
- PuppeteerContextUtils- PuppeteerCrawlingContext
 
Index
Properties
Methods
Properties
inheritedaddRequests
Type declaration
- Parameters- requestsLike: readonly (string | ReadonlyObjectDeep<Partial<RequestOptions<Dictionary>> & { regex?: RegExp; requestsFromUrl?: string }> | ReadonlyObjectDeep<Request<Dictionary>>)[]
- optionaloptions: ReadonlyObjectDeep<RequestQueueOperationOptions>- Options for the request queue 
 - Returns Promise<void>
 
inheritedbrowserController
inheritedcrawler
inheritedgetKeyValueStore
Get a key-value store with given name or id, or the default one for the crawler.
Type declaration
- Parameters- optionalidOrName: string
 - Returns Promise<KeyValueStore>
 
inheritedid
inheritedlog
A preconfigured logger for the request handler.
inheritedpage
optionalinheritedproxyInfo
An object with information about currently used proxy by the crawler and configured by the ProxyConfiguration class.
inheritedrequest
The original Request object.
optionalinheritedresponse
optionalinheritedsession
inheriteduseState
Returns the state - a piece of mutable persistent data shared across all the request handler runs.
Type declaration
- Parameters- optionaldefaultValue: State
 - Returns Promise<State>
 
Methods
inheritedaddInterceptRequestHandler
- Adds request interception handler in similar to - page.on('request', handler);but in addition to that supports multiple parallel handlers.- All the handlers are executed sequentially in the order as they were added. Each of the handlers must call one of - request.continue(),- request.abort()and- request.respond(). In addition to that any of the handlers may modify the request object (method, postData, headers) by passing its overrides to- request.continue(). If multiple handlers modify same property then the last one wins. Headers are merged separately so you can override only a value of specific header.- If one the handlers calls - request.abort()or- request.respond()then request is not propagated further to any of the remaining handlers.- Example usage: - preNavigationHooks: [
 async ({ addInterceptRequestHandler }) => {
 // Replace images with placeholder.
 await addInterceptRequestHandler((request) => {
 if (request.resourceType() === 'image') {
 return request.respond({
 statusCode: 200,
 contentType: 'image/jpeg',
 body: placeholderImageBuffer,
 });
 }
 return request.continue();
 });
 // Abort all the scripts.
 await addInterceptRequestHandler((request) => {
 if (request.resourceType() === 'script') return request.abort();
 return request.continue();
 });
 // Change requests to post.
 await addInterceptRequestHandler((request) => {
 return request.continue({
 method: 'POST',
 });
 });
 }),
 ],- Parameters- handler: InterceptHandler- Request interception handler. 
 - Returns Promise<void>
inheritedblockRequests
- Forces the Puppeteer browser tab to block loading URLs that match a provided pattern. This is useful to speed up crawling of websites, since it reduces the amount of data that needs to be downloaded from the web, but it may break some websites or unexpectedly prevent loading of resources. - By default, the function will block all URLs including the following patterns: - [".css", ".jpg", ".jpeg", ".png", ".svg", ".gif", ".woff", ".pdf", ".zip"]- If you want to extend this list further, use the - extraUrlPatternsoption, which will keep blocking the default patterns, as well as add your custom ones. If you would like to block only specific patterns, use the- urlPatternsoption, which will override the defaults and block only URLs with your custom patterns.- This function does not use Puppeteer's request interception and therefore does not interfere with browser cache. It's also faster than blocking requests using interception, because the blocking happens directly in the browser without the round-trip to Node.js, but it does not provide the extra benefits of request interception. - The function will never block main document loads and their respective redirects. - Example usage - preNavigationHooks: [
 async ({ blockRequests }) => {
 // Block all requests to URLs that include `adsbygoogle.js` and also all defaults.
 await blockRequests({
 extraUrlPatterns: ['adsbygoogle.js'],
 }),
 }),
 ],- Parameters- optionaloptions: BlockRequestsOptions
 - Returns Promise<void>
inheritedblockResources
- blockResources()has a high impact on performance in recent versions of Puppeteer. Until this resolves, please use- utils.puppeteer.blockRequests().- Parameters- optionalresourceTypes: string[]
 - Returns Promise<void>
inheritedcacheResponses
- NOTE: In recent versions of Puppeteer using this function entirely disables browser cache which resolves in sub-optimal performance. Until this resolves, we suggest just relying on the in-browser cache unless absolutely necessary. - Enables caching of intercepted responses into a provided object. Automatically enables request interception in Puppeteer. IMPORTANT: Caching responses stores them to memory, so too loose rules could cause memory leaks for longer running crawlers. This issue should be resolved or atleast mitigated in future iterations of this feature. - Parameters- cache: Dictionary<Partial<ResponseForRequest>>- Object in which responses are stored 
- responseUrlRules: (string | RegExp)[]- List of rules that are used to check if the response should be cached. String rules are compared as page.url().includes(rule) while RegExp rules are evaluated as rule.test(page.url()). 
 - Returns Promise<void>
inheritedcloseCookieModals
- Tries to close cookie consent modals on the page. Based on the I Don't Care About Cookies browser extension. - Returns Promise<void>
inheritedcompileScript
- Compiles a Puppeteer script into an async function that may be executed at any time by providing it with the following object: - {
 page: Page,
 request: Request,
 }- Where - pageis a Puppeteer- Pageand- requestis a Request.- The function is compiled by using the - scriptStringparameter as the function's body, so any limitations to function bodies apply. Return value of the compiled function is the return value of the function body = the- scriptStringparameter.- As a security measure, no globals such as - processor- requireare accessible from within the function body. Note that the function does not provide a safe sandbox and even though globals are not easily accessible, malicious code may still execute in the main process via prototype manipulation. Therefore you should only use this function to execute sanitized or safe code.- Custom context may also be provided using the - contextparameter. To improve security, make sure to only pass the really necessary objects to the context. Preferably making secured copies beforehand.- Parameters- scriptString: string
- optionalctx: Dictionary
 - Returns CompiledScriptFunction
inheritedenqueueLinks
- This function automatically finds and enqueues links from the current page, adding them to the RequestQueue currently used by the crawler. - Optionally, the function allows you to filter the target links' URLs using an array of globs or regular expressions and override settings of the enqueued Request objects. - Check out the Crawl a website with relative links example for more details regarding its usage. - Example usage - async requestHandler({ enqueueLinks }) {
 await enqueueLinks({
 globs: [
 'https://www.example.com/handbags/*',
 ],
 });
 },- Parameters- optionaloptions: ReadonlyObjectDeep<Omit<EnqueueLinksOptions, requestQueue>> & Pick<EnqueueLinksOptions, requestQueue>- All - enqueueLinks()parameters are passed via an options object.
 - Returns Promise<BatchAddRequestsResult>- Promise that resolves to BatchAddRequestsResult object. 
inheritedenqueueLinksByClickingElements
- The function finds elements matching a specific CSS selector in a Puppeteer page, clicks all those elements using a mouse move and a left mouse button click and intercepts all the navigation requests that are subsequently produced by the page. The intercepted requests, including their methods, headers and payloads are then enqueued to a provided RequestQueue. This is useful to crawl JavaScript heavy pages where links are not available in - hrefelements, but rather navigations are triggered in click handlers. If you're looking to find URLs in- hrefattributes of the page, see enqueueLinks.- Optionally, the function allows you to filter the target links' URLs using an array of PseudoUrl objects and override settings of the enqueued Request objects. - IMPORTANT: To be able to do this, this function uses various mutations on the page, such as changing the Z-index of elements being clicked and their visibility. Therefore, it is recommended to only use this function as the last operation in the page. - USING HEADFUL BROWSER: When using a headful browser, this function will only be able to click elements in the focused tab, effectively limiting concurrency to 1. In headless mode, full concurrency can be achieved. - PERFORMANCE: Clicking elements with a mouse and intercepting requests is not a low level operation that takes nanoseconds. It's not very CPU intensive, but it takes time. We strongly recommend limiting the scope of the clicking as much as possible by using a specific selector that targets only the elements that you assume or know will produce a navigation. You can certainly click everything by using the - *selector, but be prepared to wait minutes to get results on a large and complex page.- Example usage - async requestHandler({ enqueueLinksByClickingElements }) {
 await enqueueLinksByClickingElements({
 selector: 'a.product-detail',
 globs: [
 'https://www.example.com/handbags/**'
 'https://www.example.com/purses/**'
 ],
 });
 });- Parameters- options: Omit<EnqueueLinksByClickingElementsOptions, requestQueue | page>
 - Returns Promise<BatchAddRequestsResult>- Promise that resolves to BatchAddRequestsResult object. 
inheritedinfiniteScroll
- Scrolls to the bottom of a page, or until it times out. Loads dynamic content when it hits the bottom of a page, and then continues scrolling. - Parameters- optionaloptions: InfiniteScrollOptions
 - Returns Promise<void>
inheritedinjectFile
- Injects a JavaScript file into current - page. Unlike Puppeteer's- addScriptTagfunction, this function works on pages with arbitrary Cross-Origin Resource Sharing (CORS) policies.- File contents are cached for up to 10 files to limit file system access. - Parameters- filePath: string
- optionaloptions: InjectFileOptions
 - Returns Promise<unknown>
inheritedinjectJQuery
- Injects the jQuery library into current - page. jQuery is often useful for various web scraping and crawling tasks. For example, it can help extract text from HTML elements using CSS selectors.- Beware that the injected jQuery object will be set to the - window.$variable and thus it might cause conflicts with other libraries included by the page that use the same variable name (e.g. another version of jQuery). This can affect functionality of page's scripts.- The injected jQuery will survive page navigations and reloads. - Example usage: - async requestHandler({ page, injectJQuery }) {
 await injectJQuery();
 const title = await page.evaluate(() => {
 return $('head title').text();
 });
 });- Note that - injectJQuery()does not affect the Puppeteer's- page.$()function in any way.- Returns Promise<unknown>
inheritedparseWithCheerio
- Returns Cheerio handle for - page.content(), allowing to work with the data same way as with CheerioCrawler. When provided with the- selectorargument, it waits for it to be available first.- Example usage: - async requestHandler({ parseWithCheerio }) {
 const $ = await parseWithCheerio();
 const title = $('title').text();
 });- Parameters- optionalselector: string
- optionaltimeoutMs: number
 - Returns Promise<CheerioAPI>
inheritedpushData
- This function allows you to push data to a Dataset specified by name, or the one currently used by the crawler. - Shortcut for - crawler.pushData().- Parameters- optionaldata: ReadonlyDeep<Dictionary | Dictionary[]>- Data to be pushed to the default dataset. 
- optionaldatasetIdOrName: string
 - Returns Promise<void>
inheritedremoveInterceptRequestHandler
- Removes request interception handler for given page. - Parameters- handler: InterceptHandler- Request interception handler. 
 - Returns Promise<void>
inheritedsaveSnapshot
- Saves a full screenshot and HTML of the current page into a Key-Value store. - Parameters- optionaloptions: SaveSnapshotOptions
 - Returns Promise<void>
inheritedsendRequest
- Fires HTTP request via - got-scraping, allowing to override the request options on the fly.- This is handy when you work with a browser crawler but want to execute some requests outside it (e.g. API requests). Check the Skipping navigations for certain requests example for more detailed explanation of how to do that. - async requestHandler({ sendRequest }) {
 const { body } = await sendRequest({
 // override headers only
 headers: { ... },
 });
 },- Parameters- optionaloverrideOptions: Partial<OptionsInit>
 - Returns Promise<Response<Response>>
inheritedwaitForSelector
- Wait for an element matching the selector to appear. Timeout defaults to 5s. - Example usage: - async requestHandler({ waitForSelector, parseWithCheerio }) {
 await waitForSelector('article h1');
 const $ = await parseWithCheerio();
 const title = $('title').text();
 });- Parameters- selector: string
- optionaltimeoutMs: number
 - Returns Promise<void>
Add requests directly to the request queue.