今晚想给偶像的相册截个图,避免某一天网站挂了我想看看回忆都不行,用的是js的木偶师来爬虫台湾的部落格,效果图大概是这样,很不错

问题来了.我很贪心, 我想一次性把相册全爬了,也就是并发 ,这个人的相册有19个!!我一下子要开19个谷歌浏览器那个什么进程, 然后程序就崩了, 我就想能不能加入个限制? 每次只爬3个,3个完之后再继续.
ok,思路是这样:
1.假如有一个数组9个url(模拟9个相册的url),我按并发数切割,假如并发数是3

2.得到数据结构如下

3.然后循环这个数组, 再循环里面的小数组并添加进promiseChunk数组,用await Promise.all阻塞后面的其他切割的小数组运行,同时添加进AllChunksPromise数组,用来统计所有切割小数组完成后的时间,最后再Promise.all(把第二步的数据结构扁平为一维),在后面就可以计算得到代码执行时间
得到代码如下

结果还行,就是有点慢,毕竟后台调用浏览器...我的单核服务器cpu飙到100%


最后说下cheerio和puppeteer,这两个这两天都有用,cheerio解析静态网页很快,很适合抓数据。木偶可以执行js,这就很牛了,因为他是直接打开一个浏览器运行的,就是慢,各有优缺,优缺互补最好了
源代码已经上传github:https://github.com/DoctorTan/pixnetAlbumScreenshot