【问题标题】:Realtime scrape a chat using Nodejs使用 Nodejs 实时抓取聊天记录
【发布时间】:2021-02-28 17:21:39
【问题描述】:

我想要做的是在 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

【问题讨论】:

  • 应用聊天必须通过 NodeJS api 才能捕获它
  • 如果您要自动化 chrome,那么您可能必须在插入感兴趣文本的 DOM 上设置事件侦听器(然后设置从 puppeteer 到节点的基于事件的侦听器)或猴子补丁代码到他们的前端库代码中(并做同样的事情)。最好的解决方案是放弃 puppeteer 并使用他们的 API 来监听事件挂钩或广播消息的基于事件的套接字 API。

标签: node.js firebase puppeteer


【解决方案1】:

如果可能,您应该使用 API,聊天正在使用。尝试打开 Chrome 开发者工具中的网络选项卡,并尝试找出正在发生的网络请求。


如果这不可行,您可以使用MutationObserver 来监控 DOM 更改。通过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($1)', [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
        );
    });
})();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-18
    • 2016-06-26
    • 2019-07-21
    相关资源
    最近更新 更多