06 模拟不同设备
const puppeteer = require('puppeteer'); const devices = require('puppeteer/DeviceDescriptors'); const iPhone = devices['iPhone 6']; puppeteer.launch().then(async browser => { const page = await browser.newPage(); await page.emulate(iPhone); await page.goto('https://www.baidu.com'); // 其他操作... await browser.close(); });
07 请求拦截
const puppeteer = require('puppeteer'); async function run () { const browser = await puppeteer.launch({ headless:false, defaultViewport:{ width:1280, height:800 } }) const page = await browser.newPage(); await page.setRequestInterception(true); page.on('request', interceptedRequest => { const blockTypes = new Set(['image', 'media', 'font']); const type = interceptedRequest.resourceType(); const shouldBlock = blockTypes.has(type); if (shouldBlock) { interceptedRequest.abort(); } else { interceptedRequest.continue(); } }); await page.goto('https://t.zhongan.com/group'); } run();
08 性能分析
const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch(); const page = await browser.newPage(); await page.tracing.start({path: 'trace.json'}); await page.goto('https://t.zhongan.com/group'); await page.tracing.stop(); browser.close(); })();
09 生成pdf
const URL = 'http://es6.ruanyifeng.com'; const puppeteer = require('puppeteer'); const fs = require('fs'); fs.mkdirSync('es6-pdf'); (async () => { let browser = await puppeteer.launch(); let page = await browser.newPage(); await page.goto(URL); await page.waitFor(5000); // 等待五秒,确保页面加载完毕 // 获取左侧导航的所有链接地址及名字 let aTags = await page.evaluate(() => { let eleArr = [...document.querySelectorAll('#sidebar ol li a')]; return eleArr.map((a) =>{ return { href: a.href.trim(), name: a.text } }); }); // 先将本页保存成pdf,并关闭本页 console.log('正在保存:0.' + aTags[0].name); await page.pdf({path: `./es6-pdf/0.${aTags[0].name}.pdf`}); // 遍历节点数组,逐个打开并保存 (此处不再打印第一页) for (let i = 1, len = aTags.length; i < len; i++) { let a = aTags[i]; console.log('正在保存:' + i + '.' + a.name); page = await browser.newPage(); await page.goto(a.href); await page.waitFor(5000); await page.pdf({path: `./es6-pdf/${i + '.' + a.name}.pdf`}); } browser.close(); })
10 自动化发布微博
const puppeteer = require('puppeteer'); const {username,password} = require('./config') async function run(){ const browser = await puppeteer.launch({ headless:false, defaultViewport:{width:1200,height:700}, ignoreDefaultArgs:['--enable-automation'], slowMo:200, args:['--window-size=1200,700']}) const page = await browser.newPage(); await page.goto('http://wufazhuce.com/',{waitUntil:'networkidle2'}); const OneText = await page.$eval('div.fp-one-cita > a',ele=>ele.innerText); console.log('OneText:',OneText); await page.goto('https://weibo.com/',{waitUntil:'networkidle2'}); await page.waitFor(2*1000); await page.reload(); const loginUserInput = await page.waitForSelector('input#loginname'); await loginUserInput.click(); await loginUserInput.type(username); const loginUserPasswdInput = await page.waitForSelector('input[type="password"]'); await loginUserPasswdInput.click(); await loginUserPasswdInput.type(password); const loginBtn = await page.waitForSelector('a[action-type="btn_submit"]') await loginBtn.click(); const textarea = await page.waitForSelector('textarea[class="W_input"]') await textarea.click(); await textarea.type(OneText); const sendBtn = await page.waitForSelector('a[node-type="submit"]'); await sendBtn.click(); } run();
CSDN的脚本
这里注意CSDN有反扒机制,规则自己琢磨就行,我贴了伪代码,核心代码就不开放,毕竟自己玩玩就行了
const puppeteer = require('puppeteer'); async function autoCommentCSDN(username, password, targetBlogger, commentContent) { const browser = await puppeteer.launch({ headless: false }); // 打开有头浏览器 const page = await browser.newPage(); // 登录CSDN await page.goto('https://passport.csdn.net/login'); await page.waitForTimeout(1000); // 等待页面加载 // 切换到最后一个Tab (账号登录) // 点击“密码登录” const passwordLoginButton = await page.waitForXPath('//span[contains(text(), "密码登录")]'); await passwordLoginButton.click(); // 输入用户名和密码并登录 const inputFields = await page.$$('.base-input-text'); await inputFields[0].type( username); await inputFields[1].type( password); await page.click('.base-button'); await page.waitForNavigation(); // // 跳转到博主的首页 await page.goto(`https://blog.csdn.net/${targetBlogger}?type=blog`); // // 点击第一篇文章的标题,进入文章页面 await page.waitForSelector('.list-box-cont', { visible: true }); await page.click('.list-box-cont'); // // 获取文章ID console.log('page.url()',page.url()) // await page.waitForTimeout(1000); // 等待页面加载 await page.goto('https://blog.csdn.net/weixin_52898349/article/details/132115618') await page.waitForTimeout(1000); // 等待页面加载 console.log('开始点击评论按钮...') console.log('page.url()',page.url()) // 获取当前页面的DOM内容 const bodyHTML = await page.evaluate(() => { return document.body.innerHTML; }); console.log(bodyHTML); // await page.waitForSelector('.comment-side-tit'); // const commentInput = await page.$('.comment-side-tit'); // await commentInput.click(); // 等待评论按钮出现 // 点击评论按钮 // await page.waitForSelector('.comment-content'); // const commentInput = await page.$('.comment-content textarea'); // await commentInput.type(commentContent); // const submitButton = await page.$('.btn-comment-input'); // await submitButton.click(); // console.log('评论成功!'); // await browser.close(); } // 请替换以下参数为您的CSDN账号信息、目标博主和评论内容 const username = 'weixin_52898349'; const password = 'xxx!'; const targetBlogger = 'weixin_52898349'; // 目标博主的CSDN用户名 const commentContent = '各位大佬们帮忙三连一下,非常感谢!!!!!!!!!!!'; // 评论内容 autoCommentCSDN(username, password, targetBlogger, commentContent);