最近用了selenium,尽管做了很多优化,但是性能还是很差,无奈只好上网另寻他法,看了node.js也可快速加载动态网站。于是,花了两小时,学习了下nodejs,没错就是学nodejs安装与卸载。两小时后,调bug开始了,苦战cmd,苦战chrome,苦战js。战报:大窩全胜。胜后写书总结经验,分享后人再战再胜。
const puppeteer = require('puppeteer');
(async ()=> {
// chrome地址
const chromePath='C://Users//yanhui//AppData//Local//Google//Chrome//Application//chrome.exe';
//传进的将要解析的url网址
var args = process.argv.splice(2);
const address= args[0];
const browser = await puppeteer.launch({ignoreHTTPSErrors: true,headless: false,executablePath:chromePath,timeout:60000,args: ['--no-sandbox', '--disable-setuid-sandbox']});
// 打开新页面
const page = await browser.newPage();
//等待时间
var time=1000;
// 访问
await page.goto(address, {waitUntil: 'domcontentloaded'}).catch(err => console.log(err));
await page.waitFor(time);
var style,text;
page.evaluate(function(){
var style = document.createElement('style'),
text = document.createTextNode('body{background:#fff}');
style.setAttribute('type', 'text/css');
style.appendChild(text);
document.head.insertBefore(style, document.head.firstChild);
});
let content = await page.content()
console.log(content);
await browser.close();
})();
var style,text;
page.evaluate(function(){
var style = document.createElement('style'),
text = document.createTextNode('body{background:#fff}');
style.setAttribute('type', 'text/css');
style.appendChild(text);
document.head.insertBefore(style, document.head.firstChild);
});
await page.goto('http://www.baidu.com')
await page.type('#index-kw', 'puppeteer')
await page.click('#index-bn')
await page.waitForNavigation({ timeout: 3000 })
await page.screenshot({
path: 'c:/temp/baidu_iphone_X_search_puppeteer.png'
})
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({ headless: false })
const page = await browser.newPage()
await page.goto('http://www.baidu.com')
await page.type('#kw', 'puppeteer') // 键盘输入关键字
await page.waitFor(1000)
await page.click('#su') // 模拟用户点击搜索提交表单
await page.waitFor(2000)
await page.screenshot({ path: 'test/image/search.png', fullPage: true }) // 截全屏
await browser.close()
})()
const puppeteer = require('puppeteer');
const devices = require('puppeteer/DeviceDescriptors');
const iPhone = devices['iPhone 6'];
(async () => {
const browser = await puppeteer.launch({ headless: false, slowMo: 0 });
const page = await browser.newPage();
await page.emulate(iPhone); // 让页面模拟成iphone6
await page.goto('https://m.v.qq.com/play.html?cid=rjvr8psrvic4567&vid=')
await page.screenshot({ path: 'test/image/1.png' })
// 点击登录
await page.click('.btn_user_text')
await page.waitFor(1000)
await page.screenshot({ path: 'test/image/2.png' })
// 点击qq登录
await page.waitFor(1000)
await page.click('.btn_qq')
await page.waitFor(1000)
await page.screenshot({ path: 'test/image/3.png' })
// 输入账号密码
await page.type('#u', '******') // 账号
await page.type('#p', '******') // 密码
await page.screenshot({ path: 'test/image/4.png' })
await page.waitFor(1000)
// 点击登录
await page.click('#go')
await page.waitFor(1000)
await page.screenshot({ path: 'test/image/5.png' })
await page.waitFor(1000)
await browser.close();
})()
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({ headless: false, slowMo: 0 });
const page = await browser.newPage();
await page.setViewport({ // 设置viewport大小
width: 375,
height: 600,
isMobile: true,
hasTouch: true
})
await page.goto('https://www.bilibili.com/');
let list = await page.evaluate(() => { // 爬取内容
const title = document.querySelectorAll('.ri-title')
const elements = Array.from(title);
let titles = elements.map(element => {
return element.innerHTML
})
return titles
});
console.log(list)
await page.waitFor(1000) // 等待时长
await page.evaluate(() => window.scrollTo(0, document.body.scrollHeight));
await browser.close();
})()
// 性能分析
const puppeteer = require('puppeteer');
const devices = require('puppeteer/DeviceDescriptors');
const iPhone = devices['iPhone 6'];
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.emulate(iPhone); // 让页面模拟成iphone6
await page.tracing.start({ path: 'test/doc/trace.json' }); // 生成页面性能追踪的文件
await page.goto('https://www.bilibili.com/');
await page.tracing.stop();
browser.close();
})();