我希望使用木偶师浏览一个大量脚本的网站。我已经尝试过以下片段,但由于加载后续网页时超时,它将失败。
我还尝试将这个参数{waitUntil:networkidle0/networkidle2/load/domcontentloaded }包含在page.goto()中,但没有成功。
该网站有大量的广告脚本运行在后台。有人知道吗?
谢谢。
async function run() {
const browser = await puppeteer.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'] });
const page = await browser.newPage();
page.setViewport({ width: 1280, height: 2000 });
page.on('load', () => console.log('Page loaded!', page.url()));
//Goto website home page
await page.goto('https://www.discuss.com.hk');
await page.screenshot({ path: 'dis1.png' });
//goto another page in the website
await page.goto('http://www.discuss.com.hk/forumdisplay.php?fid=215');
await page.screenshot({ path: 'dis2.png' });
//Goto another page in the website
await page.goto('http://www.discuss.com.hk/forumdisplay.php?fid=1192');
await page.screenshot({ path: 'dis3.png' });
//Goto another page in the website
await page.goto('http://www.discuss.com.hk/viewthread.php?tid=27173245&extra=page%3D1');
await page.screenshot({ path: 'dis4.png' });
await browser.close();
return 'done!';
}发布于 2018-01-24 07:22:28
试着先等什么东西回来。我的经验法则是,每次我要求一个新的资源(单击一个按钮,去一个网址),我总是执行一个waitForSelector,在您的情况下,您可以使用。
await page.waitForSelector('#footer');下面的脚本工作并创建3个png文件:-
const puppeteer = require('puppeteer');
async function run() {
const browser = await puppeteer.launch( {
headless: false //true
});
const page = await browser.newPage();
await page.setViewport({ width:1280, height:2000});
await page.goto('https://www.discuss.com.hk');
await page.waitForSelector('#footer');
await page.screenshot({ path: 'dis1.png' });
await page.goto('http://www.discuss.com.hk/forumdisplay.php?fid=215');
await page.waitForSelector('#footer');
await page.screenshot({ path: 'dis2.png' });
await page.goto('http://www.discuss.com.hk/forumdisplay.php?fid=1192');
await page.waitForSelector('#footer');
await page.screenshot({ path: 'dis3.png' });
await browser.close();
};
run(); https://stackoverflow.com/questions/48404001
复制相似问题