https://www.ivx.cn/customerService/Service/
[{"insert":"通过puppeteer和ws库,本地运行爬取网站弹幕的脚本\n本地下载谷歌浏览器 https://www.google.cn/chrome/\n本地下载nodejs https://nodejs.org/en/\n安装后打开cmd命令提示符\n输入node - v\n"},{"insert":{"image":"https://file3.ih5.cn/v35/files/ecd9e82d56c9df15e685fde4494e6e27_1410_244_44.png"}},{"insert":"\n有版本信息证明安装应用环境成功\n本地新建一个文件夹 cd 到这个目录\n"},{"insert":{"image":"https://file3.ih5.cn/v35/files/e19c75c72f725462fb1d1534c4c0c067_3383_356_90.png"}},{"insert":"\n新建一个文件命名为 tanmu.js\n"},{"insert":{"image":"https://file3.ih5.cn/v35/files/e3e50dcad74e9d40b51d7f8a1144da46_2903_239_99.png"}},{"insert":"\n安装ws库\nnpm install ws\n"},{"insert":{"image":"https://file3.ih5.cn/v35/files/7c087283e6d7d6fae1a2f409cf1a55c1_1342_283_27.png"}},{"insert":"\n安装puppeteer库\nnpm i puppeteer\n"},{"insert":{"image":"https://file3.ih5.cn/v35/files/a60d1b2aa79063da74ca31b7625f6c69_1648_276_28.png"}},{"insert":"\n\nws库文档地址:https://www.npmjs.com/package/ws\npuppeteer库文档地址:https://www.npmjs.com/package/puppeteer\n\n运行脚本\nnode tanmu.js\n"},{"insert":{"image":"https://file3.ih5.cn/v35/files/7d4afbeb93ad54ae97d0762cb8f66f83_1828_259_31.png"}},{"insert":"\n\n脚本运行成功\n"},{"insert":{"image":"//file3.ih5.cn/v35/files/c2b552d473e59d87a958864f263d407e_85339_1099_730.png"}},{"insert":"\n\n\nws监听8888端口\n应用里面添加函数,实现本地连接,异步调用来接收tanmu.js 发送过来的内容\n"},{"insert":{"image":"https://file3.ih5.cn/v35/files/c911ffac08d2ff2926ffde9ab9891e09_26832_1305_216.png"}},{"insert":"\n"},{"insert":{"image":"https://file3.ih5.cn/v35/files/9e131f91afc8f477d6616ed0c43032be_926_107_25.png"}},{"insert":"\n"},{"insert":{"image":"https://file3.ih5.cn/v35/files/128fa2cdbd126c0ce1163b2b189cde6e_28014_641_95.png"}},{"insert":"\n\n转发弹幕函数 代码如下\nconst ws = new WebSocket('ws://localhost:8888');\nws.onopen = () => { console.log('WebSocket onopen'); ws.send(\"111\");}\nws.onmessage = e => { console.log('WebSocket onmessage'); _funcCb(true,e.data)}\nws.onclose = e => { console.log(\"WebSocket onclose\");};\n\n\n\n打开一个浏览器,headless设置为true时,浏览器在后台运行,不出现浏览器窗口\nconst browser = await puppeteer.launch({ headless: false });\n\n创建一个标签页\nconst page = await browser.newPage();\n\n修改页面设备模拟参数,这里选是模拟手机\nawait page.emulate({viewport:{width:600,height:800,deviceScaleFactor:1,isMobile:true,hasTouch:false,isLandscape:false},userAgent:'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4427.0 Mobile Safari/537.36'})\n\n页面打开填入一个弹幕的网址\nawait page.goto('https://www.huya.com/uzi');\n\n\n筛选某条弹幕\npage.$$eval(selector, pageFunction[, ...args])\n修改selector\n打开某个有弹幕的网站,这里选某牙的直播,打开控制台,选的模拟手机模式,查看元素找到对应的弹幕位置,确定这个selector为\n'#tanmuScroll ul li:last-child'\n获取某一条的弹幕的outerHTML然后通过ws发送出去\nfunction getDanmu(){\n page.$eval('#tanmuScroll ul li:last-child', e => e.outerHTML).then(\n (DOMString)=>{\n //console.log('aaaaaaa:', textContent); \n if (msgBuffer.length>500) {msgBuffer = []}\n if (msgBuffer.indexOf(DOMString)==-1){\n msgBuffer.push(DOMString)\n //console.log(msgBuffer)\n sendData(DOMString)\n } \n getDanmu()\n }\n )\n }\n getDanmu() \n\n"},{"insert":{"image":"https://file3.ih5.cn/v35/files/90d9c646d3aa788def792d24f82b16ba_66897_1153_457.png"}},{"insert":"\n\ntanmu.js 代码如下\n\n\nconst WebSocket = require('ws');\nconst puppeteer = require('puppeteer');\n\n\nconst wss = new WebSocket.Server({ port: 8888 });\n\nvar wss_clients_count = 0\n\n\nwss.on('connection', function connection(ws) {\n ws.on('message', function incoming(message) {\n console.log('received: %s', message);\n });\n\n ws.on('close', function closed(e) {\n console.log('closed: %s', e);\n wss_clients_count -= 1;\n });\n \n ws.send('from server connnection');\n\n ws.isAlive = true;\n ws.on('pong', heartbeat); \n\n wss_clients_count += 1;\n});\n\nfunction noop() {}\n \nfunction heartbeat() {\n this.isAlive = true;\n}\n\nconst interval = setInterval(function ping() {\n wss.clients.forEach(function each(ws) {\n if (ws.isAlive === false) {\n console.log('ws gone');\n //wss_clients_count -= 1; \n return ws.terminate();\n }\n \n ws.isAlive = false;\n ws.ping(noop);\n });\n}, 30000);\n \nwss.on('close', function close() {\n clearInterval(interval);\n});\n\n\nfunction sendData(data){\n //console.log('sendData')\n wss.clients.forEach((client) => {\n if (client.readyState === WebSocket.OPEN) {\n client.send(data);\n }\n }); \n \n}\n\nvar msgBuffer = [];\n\n\n(async () => {\n const browser = await puppeteer.launch({ headless: false });\n const page = await browser.newPage();\n await page.emulate({viewport:{width:600,height:800,deviceScaleFactor:1,isMobile:true,hasTouch:false,isLandscape:false},userAgent:'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4427.0 Mobile Safari/537.36'})\n \n await page.goto('https://www.huya.com/uzi');\n\n \n function getDanmu(){\n page.$eval('#tanmuScroll ul li:last-child', e => e.outerHTML).then(\n (DOMString)=>{\n //console.log('aaaaaaa:', textContent); \n if (msgBuffer.length>500) {msgBuffer = []}\n if (msgBuffer.indexOf(DOMString)==-1){\n msgBuffer.push(DOMString)\n //console.log(msgBuffer)\n sendData(DOMString)\n } \n getDanmu()\n }\n )\n }\n console.log('获取弹幕中')\n getDanmu() \n \n})();\n\n测试预览地址:https://v4pre.h5sys.cn/play/4vwjNbpb"},{"insert":{"image":"https://file3.ih5.cn/v35/files/e529479161ca1bbf68f5c956a799690a_91304_1285_653.png"}},{"insert":"\n\n\n\n\n\n\n\n\n"}]
通过puppeteer和ws库,本地运行爬取网站弹幕的脚本 本地下载谷歌浏览器https://www.google.cn/chrome/ 本地下载nodejshttps://nodejs.org/en/ 安装后打开cmd命令提示符 输入node-v 有版本信息证明安装应用环境成功 本地新建一个文件夹cd到这个目录 新建一个文件命名为tanmu.js 安装ws库 npminstallws 安装puppeteer库 npmipuppeteer ws库文档地址:https://www.npmjs.com/package/ws puppeteer库文档地址:https://www.npmjs.com/package/puppeteer 运行脚本 nodetanmu.js 脚本运行成功 ws监听8888端口 应用里面添加函数,实现本地连接,异步调用来接收tanmu.js发送过来的内容 转发弹幕函数代码如下 constws=newWebSocket('ws://localhost:8888'); ws.onopen=()=>{ console.log('WebSocketonopen'); ws.send("111");} ws.onmessage=e=>{ console.log('WebSocketonmessage'); _funcCb(true,e.data)} ws.onclose=e=>{ console.log("WebSocketonclose");}; 打开一个浏览器,headless设置为true时,浏览器在后台运行,不出现浏览器窗口 const browser = await puppeteer.launch({ headless: false }); 创建一个标签页 const page = await browser.newPage(); 修改页面设备模拟参数,这里选是模拟手机 await page.emulate({viewport:{width:600,height:800,deviceScaleFactor:1,isMobile:true,hasTouch:false,isLandscape:false},userAgent:'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4427.0 Mobile Safari/537.36'}) 页面打开填入一个弹幕的网址 await page.goto('https://www.huya.com/uzi'); 筛选某条弹幕 page.$$eval(selector,pageFunction[,...args]) 修改selector 打开某个有弹幕的网站,这里选某牙的直播,打开控制台,选的模拟手机模式,查看元素找到对应的弹幕位置,确定这个selector为 '#tanmuScroll ul li:last-child' 获取某一条的弹幕的outerHTML然后通过ws发送出去 function getDanmu(){ page.$eval('#tanmuScroll ul li:last-child', e => e.outerHTML).then( (DOMString)=>{ //console.log('aaaaaaa:', textContent); if (msgBuffer.length>500) {msgBuffer = []} if (msgBuffer.indexOf(DOMString)==-1){ msgBuffer.push(DOMString) //console.log(msgBuffer) sendData(DOMString) } getDanmu() } ) } getDanmu() tanmu.js代码如下 const WebSocket = require('ws'); const puppeteer = require('puppeteer'); const wss = new WebSocket.Server({ port: 8888 }); var wss_clients_count = 0 wss.on('connection', function connection(ws) { ws.on('message', function incoming(message) { console.log('received: %s', message); }); ws.on('close', function closed(e) { console.log('closed: %s', e); wss_clients_count -= 1; }); ws.send('from server connnection'); ws.isAlive = true; ws.on('pong', heartbeat); wss_clients_count += 1; }); function noop() {} function heartbeat() { this.isAlive = true; } const interval = setInterval(function ping() { wss.clients.forEach(function each(ws) { if (ws.isAlive === false) { console.log('ws gone'); //wss_clients_count -= 1; return ws.terminate(); } ws.isAlive = false; ws.ping(noop); }); }, 30000); wss.on('close', function close() { clearInterval(interval); }); function sendData(data){ //console.log('sendData') wss.clients.forEach((client) => { if (client.readyState === WebSocket.OPEN) { client.send(data); } }); } var msgBuffer = []; (async () => { const browser = await puppeteer.launch({ headless: false }); const page = await browser.newPage(); await page.emulate({viewport:{width:600,height:800,deviceScaleFactor:1,isMobile:true,hasTouch:false,isLandscape:false},userAgent:'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4427.0 Mobile Safari/537.36'}) await page.goto('https://www.huya.com/uzi'); function getDanmu(){ page.$eval('#tanmuScroll ul li:last-child', e => e.outerHTML).then( (DOMString)=>{ //console.log('aaaaaaa:', textContent); if (msgBuffer.length>500) {msgBuffer = []} if (msgBuffer.indexOf(DOMString)==-1){ msgBuffer.push(DOMString) //console.log(msgBuffer) sendData(DOMString) } getDanmu() } ) } console.log('获取弹幕中') getDanmu() })(); 测试预览地址:https://v4pre.h5sys.cn/play/4vwjNbpb
爬取网站的弹幕通过puppeteer和ws库,本地运行爬取网站弹幕的脚本 本地下载谷歌浏览器https://www.google.cn/chrome/ 本地下载nodejshttps://nodejs.org/en/ 安装后打开cmd命令提示符 输入node-v 有版本信息证明安装应用环境成功 本地新建一个文件夹cd到这个目录 新建一个文件命名为tanmu.js 安装ws库 npminstallws 安装puppeteer库 npmipuppeteer ws库文档地址:https://www.npmjs.com/package/ws puppeteer库文档地址:https://www.npmjs.com/package/puppeteer 运行脚本 nodetanmu.js 脚本运行成功 ws监听8888端口 应用里面添加函数,实现本地连接,异步调用来接收tanmu.js发送过来的内容 转发弹幕函数代码如下 constws=newWebSocket('ws://localhost:8888'); ws.onopen=()=>{ console.log('WebSocketonopen'); ws.send("111");} ws.onmessage=e=>{ console.log('WebSocketonmessage'); _funcCb(true,e.data)} ws.onclose=e=>{ console.log("WebSocketonclose");}; 打开一个浏览器,headless设置为true时,浏览器在后台运行,不出现浏览器窗口 const browser = await puppeteer.launch({ headless: false }); 创建一个标签页 const page = await browser.newPage(); 修改页面设备模拟参数,这里选是模拟手机 await page.emulate({viewport:{width:600,height:800,deviceScaleFactor:1,isMobile:true,hasTouch:false,isLandscape:false},userAgent:'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4427.0 Mobile Safari/537.36'}) 页面打开填入一个弹幕的网址 await page.goto('https://www.huya.com/uzi'); 筛选某条弹幕 page.$$eval(selector,pageFunction[,...args]) 修改selector 打开某个有弹幕的网站,这里选某牙的直播,打开控制台,选的模拟手机模式,查看元素找到对应的弹幕位置,确定这个selector为 '#tanmuScroll ul li:last-child' 获取某一条的弹幕的outerHTML然后通过ws发送出去 function getDanmu(){ page.$eval('#tanmuScroll ul li:last-child', e => e.outerHTML).then( (DOMString)=>{ //console.log('aaaaaaa:', textContent); if (msgBuffer.length>500) {msgBuffer = []} if (msgBuffer.indexOf(DOMString)==-1){ msgBuffer.push(DOMString) //console.log(msgBuffer) sendData(DOMString) } getDanmu() } ) } getDanmu() tanmu.js代码如下 const WebSocket = require('ws'); const puppeteer = require('puppeteer'); const wss = new WebSocket.Server({ port: 8888 }); var wss_clients_count = 0 wss.on('connection', function connection(ws) { ws.on('message', function incoming(message) { console.log('received: %s', message); }); ws.on('close', function closed(e) { console.log('closed: %s', e); wss_clients_count -= 1; }); ws.send('from server connnection'); ws.isAlive = true; ws.on('pong', heartbeat); wss_clients_count += 1; }); function noop() {} function heartbeat() { this.isAlive = true; } const interval = setInterval(function ping() { wss.clients.forEach(function each(ws) { if (ws.isAlive === false) { console.log('ws gone'); //wss_clients_count -= 1; return ws.terminate(); } ws.isAlive = false; ws.ping(noop); }); }, 30000); wss.on('close', function close() { clearInterval(interval); }); function sendData(data){ //console.log('sendData') wss.clients.forEach((client) => { if (client.readyState === WebSocket.OPEN) { client.send(data); } }); } var msgBuffer = []; (async () => { const browser = await puppeteer.launch({ headless: false }); const page = await browser.newPage(); await page.emulate({viewport:{width:600,height:800,deviceScaleFactor:1,isMobile:true,hasTouch:false,isLandscape:false},userAgent:'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4427.0 Mobile Safari/537.36'}) await page.goto('https://www.huya.com/uzi'); function getDanmu(){ page.$eval('#tanmuScroll ul li:last-child', e => e.outerHTML).then( (DOMString)=>{ //console.log('aaaaaaa:', textContent); if (msgBuffer.length>500) {msgBuffer = []} if (msgBuffer.indexOf(DOMString)==-1){ msgBuffer.push(DOMString) //console.log(msgBuffer) sendData(DOMString) } getDanmu() } ) } console.log('获取弹幕中') getDanmu() })(); 测试预览地址:https://v4pre.h5sys.cn/play/4vwjNbpb