扫一扫分享
apify-js是一款用于 JavaScript 的可伸缩的 web 爬虫和抓取库。能通过无头(headless)Chrome 和 Puppeteer 实现数据提取和 Web 自动化作业的开发。 它提供了管理和自动扩展无头Chrome / Puppeteer实例池的工具,维护要爬网的URL队列,将爬网结果存储到本地文件系统或云端。
安装:
npm install apify --save
运行以下示例以使用Puppeteer执行网站的爬取:
const Apify = require('apify');
Apify.main(async () => {
const requestQueue = await Apify.openRequestQueue();
await requestQueue.addRequest({ url: 'https://www.iana.org/' });
const pseudoUrls = [new Apify.PseudoUrl('https://www.iana.org/[.*]')];
const crawler = new Apify.PuppeteerCrawler({
requestQueue,
handlePageFunction: async ({ request, page }) => {
const title = await page.title();
console.log(`Title of ${request.url}: ${title}`);
await Apify.utils.puppeteer.enqueueLinks(page, 'a', pseudoUrls, requestQueue);
},
maxRequestsPerCrawl: 100,
maxConcurrency: 10,
});
await crawler.run();
});
手机预览