Puppeteer 用處
配置
Node環境配置
下載并安裝NodeJS:
wget https://nodejs.org/dist/v8.12.0/node-v8.12.0-linux-x64.tar.xz tar xf node-v8.12.0-linux-x64.tar.xz mv node-v8.12.0-linux-x64 /usr/local/lib ln -s /usr/local/lib/node-v8.12.0-linux-x64/bin/npm /usr/local/bin/ ln -s /usr/local/lib/node-v8.12.0-linux-x64/bin/node /usr/local/bin/
*(可選)配置淘寶的源,加速npm包的下載:
npm config set registry https://registry.npm.taobao.org
安裝Puppeteer
配置淘寶的Puppeteer下載源,用于安裝Chromium:
export PUPPETEER_DOWNLOAD_HOST=https://storage.googleapis.com.cnpmjs.org npm i puppeteer
國內不配置時會在卡在下載Chromium
示例
新建一個test.js
const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch({ ignoreHTTPSErrors: true, headless: false, args: ['--no-sandbox'] }); const page = await browser.newPage(); await page.goto('https://intest.tech'); await page.waitFor(5e3); await browser.close(); })();
運行:
node test.js
截圖
知識點
const puppeteer = require('puppeteer'); (async () => { const browser = await (puppeteer.launch({ // 若是手動下載的chromium需要指定chromium地址, 默認引用地址為 /項目目錄/node_modules/puppeteer/.local-chromium/ executablePath: '/Users/huqiyang/Documents/project/z/chromium/Chromium.app/Contents/MacOS/Chromium', //設置超時時間 timeout: 15000, //如果是訪問https頁面 此屬性會忽略https錯誤 ignoreHTTPSErrors: true, // 打開開發者工具, 當此值為true時, headless總為false devtools: false, // 關閉headless模式, 會打開瀏覽器 headless: false })); const page = await browser.newPage(); await page.goto('https://www.jianshu.com/u/40909ea33e50'); await page.screenshot({ path: 'jianshu.png', type: 'png', // quality: 100, 只對jpg有效 fullPage: true, // 指定區域截圖,clip和fullPage兩者只能設置一個 // clip: { // x: 0, // y: 0, // width: 1000, // height: 40 // } }); browser.close(); })();
進階,獲取網易云音樂的歌詞和評論
網易云音樂的API經過AES和RSA算法加密,需要攜帶加密的信息通過POST方式請求才能獲取到數據。但 Puppeteer 出現后,這些都不重要了,只要它頁面上顯示了,通過 Puppeteer 都能獲取到該元素。
知識點
const fs = require('fs'); const puppeteer = require('puppeteer'); (async () => { const browser = await (puppeteer.launch({ executablePath: '/Users/huqiyang/Documents/project/z/chromium/Chromium.app/Contents/MacOS/Chromium', headless: false })); const page = await browser.newPage(); // 進入頁面 await page.goto('https://music.163.com/#'); // 點擊搜索框擬人輸入 鬼才會想起 const musicName = '鬼才會想'; await page.type('.txt.j-flag', musicName, {delay: 0}); // 回車 await page.keyboard.press('Enter'); // 獲取歌曲列表的 iframe await page.waitFor(2000); let iframe = await page.frames().find(f => f.name() === 'contentFrame'); const SONG_LS_SELECTOR = await iframe.$('.srchsongst'); // 獲取歌曲 鬼才會想起 的地址 const selectedSongHref = await iframe.evaluate(e => { const songList = Array.from(e.childNodes); const idx = songList.findIndex(v => v.childNodes[1].innerText.replace(/\s/g, '') === '鬼才會想起'); return songList[idx].childNodes[1].firstChild.firstChild.firstChild.href; }, SONG_LS_SELECTOR); // 進入歌曲頁面 await page.goto(selectedSongHref); // 獲取歌曲頁面嵌套的 iframe await page.waitFor(2000); iframe = await page.frames().find(f => f.name() === 'contentFrame'); // 點擊 展開按鈕 const unfoldButton = await iframe.$('#flag_ctrl'); await unfoldButton.click(); // 獲取歌詞 const LYRIC_SELECTOR = await iframe.$('#lyric-content'); const lyricCtn = await iframe.evaluate(e => { return e.innerText; }, LYRIC_SELECTOR); console.log(lyricCtn); // 截圖 await page.screenshot({ path: '歌曲.png', fullPage: true, }); // 寫入文件 let writerStream = fs.createWriteStream('歌詞.txt'); writerStream.write(lyricCtn, 'UTF8'); writerStream.end(); // 獲取評論數量 const commentCount = await iframe.$eval('.sub.s-fc3', e => e.innerText); console.log(commentCount); // 獲取評論 const commentList = await iframe.$$eval('.itm', elements => { const ctn = elements.map(v => { return v.innerText.replace(/\s/g, ''); }); return ctn; }); console.log(commentList); })();
參考
https://github.com/cnpm/cnpmjs.org/issues/1246#issuecomment-341631992
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com