服務器拿到模擬瀏覽器渲染的結果,可模擬dom操作。
const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch(); const page = await browser.newPage(); await page.goto('https://www.tucao.one/index.php?m=member&c=index&a=login&forward='); //console.log輸出內容 page.on('console', msg => console.log('console.log:', msg.text())); //注入腳本 const dimensions = await page.evaluate(async () => { //將js注入網頁 async function timeout(t) { return new Promise((resolve, reject) => { setTimeout(() => resolve(), t); }); } await timeout(100) console.log('網頁中輸入') return { cookie: document.cookie, // content: $('body').html(), } }); console.log('輸出內容:', dimensions); await browser.close(); })();
能拿到網頁中的對象數據。