Crawler Puppeteer avatar
Crawler Puppeteer

No credit card required

View all Actors
Crawler Puppeteer

Crawler Puppeteer

jancurn/crawler-puppeteer

No credit card required

Copy of https://github.com/apifytech/actor-crawler-puppeteer

Apify Crawler Puppeteer

How it works

Crawler Puppeteer is the most powerful crawler tool in our arsenal (aside from developing your own actors). It uses the Puppeteer library to programmatically control a headless Chrome browser and it can make it do almost anything. If using the Crawler does not cut it, Crawler Puppeteer is what you need.

The downside is that Puppeteer is a Node.js library, so knowledge of Node.js and its paradigms is expected when working with the Crawler Puppeteer.

If you need either a more performant, or a simpler tool, see the crawler-cheerio for unmatched performance, or crawler for a plain old JavaScript tool.

Input

Input is provided via the pre-configured UI. See the tooltips for more info on the available options.

Page function

Page function is a single JavaScript function that enables the user to control the Crawler's operation, manipulate the crawled pages and extract data as needed. It is invoked with a context object containing the following properties:

1const context = {
2    // USEFUL DATA
3    input, // Unaltered original input as parsed from the UI
4    env, // Contains information about the run such as actorId or runId
5    customData, // Value of the 'Custom data' Crawler option.
6    request, // Apify.Request object.
7    response, // Response object holding the status code and headers.
8    
9    // EXPOSED FUNCTIONS
10    saveSnapshot, // Saves a screenshot and full HTML of the current page to the key value store.
11    skipLinks, // Prevents enqueueing more links via Pseudo URLs on the current page.
12    skipOutput, // Prevents saving the return value of the pageFunction to the default dataset.
13    enqueuePage, // Adds a page to the request queue.
14    jQuery, // A reference to the jQuery $ function (if injectJQuery was used).
15    
16    // EXPOSED OBJECTS
17    globalStore, // Represents an in memory store that can be used to share data across pageFunction invocations.
18    requestList, // Reference to the run's default Apify.RequestList.
19    requestQueue, // Reference to the run's default Apify.RequestQueue.
20    dataset, // Reference to the run's default Apify.Dataset.
21    keyValueStore, // Reference to the run's default Apify.KeyValueStore.
22    log, // Reference to Apify.utils.log 
23    underscoreJs, // A reference to the Underscore _ object (if injectUnderscore was used).
24}

context

The following tables describe the context object in more detail.

Data structures:

ArgumentType
inputstring
Raw input as it was received from the UI, represented as a string for immutability. You can JSON.parse() it to get the values of individual configuration options.
envObject
A map of all the relevant environment variables that you may want to use. See the Apify.getEnv() function for a preview of the structure and full documentation.
customDataObject
Since the input UI is fixed, it does not support adding of other fields that may be needed for all specific use cases. If you need to pass arbitrary data to the crawler, use the Custom data input field and its contents will be available under the customData context key.
requestRequest
Apify uses a request object to represent metadata about the currently crawled page, such as its URL or the number of retries. See the Request class for a preview of the structure and full documentation.
response{status: number, headers: Object}
The HTTP response object is produced by Puppeteer. Currently, we only pass the HTTP status code and the response headers to the context.

Functions:

ArgumentType
saveSnapshotFunction
A helper function that enables saving a snapshot of the current page's HTML and its screenshot into the default key value store. Each snapshot overwrites the previous one and the function's invocations will also be throttled if invoked more than once in 2 seconds, to prevent abuse. So make sure you don't call it for every single request. You can find the screenshot under the SNAPSHOT-SCREENSHOT key and the HTML under the SNAPSHOT-HTML key.
skipLinksFunction
With each invocation of the pageFunction the crawler attempts to extract new URLs from the page using the Link selector and PseudoURLs provided in the input UI. If you want to prevent this behavior in certain cases, call the skipLinks function and no URLs will be added to the queue for the given page.
skipOutputFunction
Since each return value of the pageFunction is saved to the default dataset, this provides a way of overriding that functionality. Just call skipOutput and the result of the current invocation will not be saved to the dataset.
enqueuePageFunction
To enqueue a specific URL manually instead of automatically by a combination of a Link selector and a Pseudo URL, use the enqueuePage function. It accepts a plain object as argument that needs to have the structure to construct a Request object. But frankly, you just need a URL: { url: 'https://www.example.com }
jQueryFunction
To make the DOM manipulation within the page easier, you may choose the injectJQuery option in the UI and all the crawled pages will have an instance of the jQuery library available. However, since we do not want to modify the page in any way, we don't inject it into the global $ object as you may be used to, but instead we make it available in context. Feel free to const $ = context.jQuery to get the familiar notation.

Class instances:

Global Store

globalStore represents an instance of a very simple in memory store that is not scoped to the individual pageFunction invocation. This enables you to easily share global data such as API responses, tokens and other. Since the stored data need to cross the from the Browser to the Node.js process, they cannot be any data, but always need to be JSON stringifiable. Therefore, you cannot store DOM objects, live class instances, functions etc. Only a JSON representation of the passed object will be stored, with all the relevant limitations.

MethodReturn Type
get(key:string)Promise<Object>
Retrieves a JSON serializable value from the global store using the provided key.
set(key:string, value:Object)Promise
Saves a JSON serializable value to the global store using the provided key.
size()Promise<number>
Returns the current number of values in the global store.
list()Promise<Array>
Returns all the keys currently stored in the global store.

Output

Ouput is a dataset containing extracted data for each scraped page.

Dataset

For each of the scraped URLs, the dataset contains an object with results and some metadata. If you were scraping the HTML <title> of IANA it would look like this:

1{
2  "title": "Internet Assigned Numbers Authority",
3  "#error": false,
4  "#debug": {
5    "url": "https://www.iana.org/",
6    "method": "GET",
7    "retryCount": 0,
8    "errorMessages": null,
9    "requestId": "e2Hd517QWfF4tVh"
10  }
11}

The metadata are prefixed with a #. Soon you will be able to exclude the metadata from the results by providing an API flag.

Developer
Community logoMaintained by Community
Actor metrics
  • 1 monthly users
  • Modified over 5 years ago
Categories

You might also like these Actors