使用 Nodejs 实时抓取聊天记录
Realtime scrape a chat using Nodejs
我想做的是在 NodeJs 上构建一个 scraping 应用程序,从中实时监控聊天并将某些消息存储在任何数据库中?
我想做的是以下,我想从聊天平台流中获取数据,从而获取一些有用的信息,帮助那些正在做流服务的人;
但是我不知道如何使用 NodeJs 开始做这个,
到目前为止我能做的是捕获消息的数据,但是我无法实时监控新消息,
在这方面有什么帮助吗?
到目前为止我做了什么:
server.js
var express = require('express');
var fs = require('fs');
var request = require('request');
var puppeteer = require('puppeteer');
var app = express();
app.get('/', function(req, res){
url = 'https://www.nimo.tv/live/6035521326';
(async() => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
await page.waitForSelector('.msg-nickname');
const messages = await page.evaluate(() => {
return Array.from(document.querySelectorAll('.msg-nickname'))
.map(item => item.innerText);
});
console.log(messages);
})();
res.send('Check your console!')
});
app.listen('8081')
console.log('Magic happens on port 8081');
exports = module.exports = app;
有了这个,我得到了用户消息的昵称并放入一个数组中,我想制作我的应用程序 运行 并在聊天中完成输入时自动接收新的昵称,
对这个挑战有帮助吗?
也许我需要使用 WebSocket
如果可能你应该使用API,聊天正在使用。尝试打开 Chrome 开发人员工具中的网络选项卡,并尝试找出正在发生的网络请求。
如果那不可能,您可以使用 MutationObserver
to monitor DOM changes. Expose a function via page.exposeFunction
然后听取相关的变化。然后,您可以将获得的数据插入数据库。
下面是一些帮助您入门的示例代码:
const puppeteer = require('puppeteer');
const { Client } = require('pg');
(async () => {
const client = new Client(/* ... */);
await client.connect(); // connect to database
const browser = await puppeteer.launch({ headless: false });
const [page] = await browser.pages();
// call a handler when a mutation happens
async function mutationListener(addedText) {
console.log(`Added text: ${addedText}`);
// insert data into database
await client.query('INSERT INTO users(text) VALUES()', [addedText]);
}
page.exposeFunction('mutationListener', mutationListener);
await page.goto('http://...');
await page.waitForSelector('.msg-nickname');
await page.evaluate(() => {
// wait for any mutations inside a specific element (e.g. the chatbox)
const observerTarget = document.querySelector('ELEMENT-TO-MONITOR');
const mutationObserver = new MutationObserver((mutationsList) => {
// handle change by checking which elements were added and which were deleted
for (const mutation of mutationsList) {
const { removedNodes, addedNodes } = mutation;
// example: pass innerText of first added element to our mutationListener
mutationListener(addedNodes[0].innerText);
}
});
mutationObserver.observe( // start observer
observerTarget,
{ childList: true }, // wait for new child nodes to be added/removed
);
});
})();
我想做的是在 NodeJs 上构建一个 scraping 应用程序,从中实时监控聊天并将某些消息存储在任何数据库中?
我想做的是以下,我想从聊天平台流中获取数据,从而获取一些有用的信息,帮助那些正在做流服务的人;
但是我不知道如何使用 NodeJs 开始做这个,
到目前为止我能做的是捕获消息的数据,但是我无法实时监控新消息, 在这方面有什么帮助吗?
到目前为止我做了什么:
server.js
var express = require('express');
var fs = require('fs');
var request = require('request');
var puppeteer = require('puppeteer');
var app = express();
app.get('/', function(req, res){
url = 'https://www.nimo.tv/live/6035521326';
(async() => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
await page.waitForSelector('.msg-nickname');
const messages = await page.evaluate(() => {
return Array.from(document.querySelectorAll('.msg-nickname'))
.map(item => item.innerText);
});
console.log(messages);
})();
res.send('Check your console!')
});
app.listen('8081')
console.log('Magic happens on port 8081');
exports = module.exports = app;
有了这个,我得到了用户消息的昵称并放入一个数组中,我想制作我的应用程序 运行 并在聊天中完成输入时自动接收新的昵称, 对这个挑战有帮助吗?
也许我需要使用 WebSocket
如果可能你应该使用API,聊天正在使用。尝试打开 Chrome 开发人员工具中的网络选项卡,并尝试找出正在发生的网络请求。
如果那不可能,您可以使用 MutationObserver
to monitor DOM changes. Expose a function via page.exposeFunction
然后听取相关的变化。然后,您可以将获得的数据插入数据库。
下面是一些帮助您入门的示例代码:
const puppeteer = require('puppeteer');
const { Client } = require('pg');
(async () => {
const client = new Client(/* ... */);
await client.connect(); // connect to database
const browser = await puppeteer.launch({ headless: false });
const [page] = await browser.pages();
// call a handler when a mutation happens
async function mutationListener(addedText) {
console.log(`Added text: ${addedText}`);
// insert data into database
await client.query('INSERT INTO users(text) VALUES()', [addedText]);
}
page.exposeFunction('mutationListener', mutationListener);
await page.goto('http://...');
await page.waitForSelector('.msg-nickname');
await page.evaluate(() => {
// wait for any mutations inside a specific element (e.g. the chatbox)
const observerTarget = document.querySelector('ELEMENT-TO-MONITOR');
const mutationObserver = new MutationObserver((mutationsList) => {
// handle change by checking which elements were added and which were deleted
for (const mutation of mutationsList) {
const { removedNodes, addedNodes } = mutation;
// example: pass innerText of first added element to our mutationListener
mutationListener(addedNodes[0].innerText);
}
});
mutationObserver.observe( // start observer
observerTarget,
{ childList: true }, // wait for new child nodes to be added/removed
);
});
})();