-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
86 lines (69 loc) · 3.59 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const Apify = require('apify');
// Apify.utils contains various utilities, e.g. for logging.
// Here we turn off the logging of unimportant messages.
const { log } = Apify.utils;
log.setLevel(log.LEVELS.WARNING);
// A link to a list of Fortune 500 companies' websites available on GitHub.
const CSV_LINK = 'https://gist.githubusercontent.com/hrbrmstr/ae574201af3de035c684/raw/f1000.csv';
// Apify.main() function wraps the crawler logic (it is optional).
Apify.main(async () => {
// Create an instance of the RequestList class that contains a list of URLs to crawl.
// Here we download and parse the list of URLs from an external file.
// Open a request queue and add a start URL to it
const requestQueue = await Apify.openRequestQueue();
await requestQueue.addRequest({ url: 'https://www.iana.org/' });
// Define a pattern of URLs that the crawler should visit
const pseudoUrls = [new Apify.PseudoUrl('https://www.iana.org/[.*]')];
console.log('===========================START===============================');
console.log('Crawler start.');
console.log('===============================================================');
// Create an instance of the CheerioCrawler class - a crawler
// that automatically loads the URLs and parses their HTML using the cheerio library.
const crawler = new Apify.CheerioCrawler({
// Let the crawler fetch URLs from our list.
requestQueue,
// The crawler downloads and processes the web pages in parallel, with a concurrency
// automatically managed based on the available system memory and CPU (see AutoscaledPool class).
// Here we define some hard limits for the concurrency.
minConcurrency: 10,
maxConcurrency: 50,
// On error, retry each page at most once.
maxRequestRetries: 1,
// Increase the timeout for processing of each page.
handlePageTimeoutSecs: 60,
// This function will be called for each URL to crawl.
// It accepts a single parameter, which is an object with the following fields:
// - request: an instance of the Request class with information such as URL and HTTP method
// - html: contains raw HTML of the page
// - $: the cheerio object containing parsed HTML
handlePageFunction: async ({ request, html, $ }) => {
console.log(`Processing ${request.url}...`);
// Extract data from the page using cheerio.
const title = $('title').text();
console.log($('body').html());
// const h1texts = [];
// $('h1').each((index, el) => {
// h1texts.push({
// text: $(el).text(),
// });
// });
// Store the results to the default dataset. In local configuration,
// the data will be stored as JSON files in ./apify_storage/datasets/default
// await Apify.pushData({
// url: request.url,
// title,
// h1texts,
// html,
// });
},
// This function is called if the page processing failed more than maxRequestRetries+1 times.
handleFailedRequestFunction: async ({ request }) => {
console.log(`Request ${request.url} failed twice.`);
},
});
// Run the crawler and wait for it to finish.
await crawler.run();
console.log('===========================END=================================');
console.log('Crawler finished.');
console.log('===============================================================');
});