问:现在,我使用操纵符来爬网网页上的数据。我需要使用铬。每次完成爬网数据时,chrome都会占用大量CPU和内存,如下图所示:我的代码如下:const puppeteer = require('puppeteer'); (异步()=> {
image.png
const browser = await puppeteer.launch({
args: ['--no-sandbox'],
dumpio: false
});
const page = await browser.newPage();
await page.goto('https://book.douban.com/chart');
try {
let issue = await page.$eval('div.main > div.wrap > ul.lot-award > li.currentAward > div.period > span ',el => el.innerHTML);
let num = await page.$eval('div.main > div.wrap > ul.lot-award > li.currentAward > #Num.lot-nums ',el => el.innerHTML);
let num1 = num.replace(/ /g, '');
let num2 = num1.replace(/[\@\#$\%\^\&\*\(\)\{\}\:\"\L\<\>\?\[\]]/g,'');
let num3 = num2.replace(/spanclass/g,'').replace(/span/g,'').replace(/no/g,'').replace(/=/g,'');
let num4 = num3.substring(0,num3.length - 1);
let num5 = num4.split('/');
console.log(num3);
console.log(num4);
console.log(num5);
console.log(issue);
} catch (err) {
console.log ('pid: ', process.pid ,'forwarding error: ',': \ R / N ', err.stack )
} finally {
browser.close (); // close and exit. You can comment out this line of code to see the final result
}
})();
打扰一下,我该怎么办?您有什么好的方法或想法吗?谢!!!
答:page.close()尝试查看是否可以解决问题。我不知道以下其他子进程是否在关闭浏览器后没有关闭,或者在关闭浏览器后是否没有关闭browser.close()关闭失败。