Nodejs新浪微博的评论数据抓取和设备分析

Nodejs新浪微博的评论数据抓取和设备分析

共享一套代码,新浪微博的评论数据抓取,客户端设备类型分析 https://github.com/kissliux/weibo_comment

你问我这段代码有什么用?

看这个 如何评价小米、罗永浩等指责魅族请水军发 1799 评论? http://www.zhihu.com/question/25395651/answer/30664062


6 回复

Node.js 微博评论数据抓取与设备分析

简介

本文将介绍如何使用 Node.js 抓取微博上的评论数据,并对这些数据进行简单的设备类型分析。我们将使用一个开源项目来展示如何实现这一功能。

示例代码

首先,我们需要安装一些必要的库:

npm install axios cheerio

axios 用于发送 HTTP 请求,cheerio 用于解析 HTML。

以下是一个简单的示例代码,用于抓取微博上的评论数据并分析设备类型:

const axios = require('axios');
const cheerio = require('cheerio');

async function fetchComments(url) {
    try {
        const response = await axios.get(url);
        const $ = cheerio.load(response.data);

        // 解析评论数据
        const comments = [];
        $('.list_li_Span').each((index, element) => {
            const commentText = $(element).find('.WB_text').text();
            const deviceInfo = $(element).find('.from').text();

            comments.push({
                text: commentText,
                device: deviceInfo
            });
        });

        return comments;
    } catch (error) {
        console.error('Error fetching comments:', error);
    }
}

function analyzeDevice(comments) {
    const deviceCount = {};
    comments.forEach(comment => {
        const device = comment.device;
        if (device in deviceCount) {
            deviceCount[device]++;
        } else {
            deviceCount[device] = 1;
        }
    });

    console.log('Device Analysis:', deviceCount);
}

// 示例微博链接
const weiboUrl = 'https://weibo.com/123456789/123456789';

fetchComments(weiboUrl)
    .then(comments => {
        analyzeDevice(comments);
    })
    .catch(error => {
        console.error('Failed to fetch comments:', error);
    });

代码解释

  1. 安装依赖

    • axios 用于发送 HTTP 请求。
    • cheerio 用于解析 HTML。
  2. 抓取评论数据

    • 使用 axios 发送 GET 请求获取微博页面。
    • 使用 cheerio 解析 HTML,提取评论文本和设备信息。
  3. 设备分析

    • 统计不同设备类型的出现次数。
    • 输出结果。

示例项目

该项目可以在 GitHub 上找到:kissliux/weibo_comment

通过这段代码,你可以抓取微博上的评论数据,并对其进行设备类型分析。这有助于了解用户使用的设备类型分布情况,从而更好地理解用户的使用习惯。

参考资料

希望这段代码和解释对你有所帮助!


魅族业界号称水军帝国 又不是一天两天的事了。想当年我还买了M8.

readme 里面如果有数据可视化就好了。。光是看数据列表好累

恩,他们三个相互黑吧。。。。 对比锤子,小米,魅族。。 得出的一致结论是,还是iPhone好。。

思路不错

这段代码的作用是通过爬虫技术抓取新浪微博上的评论数据,并对这些数据进行设备类型分析。具体来说,它可以统计不同类型的设备(如iPhone、Android手机等)在评论中的占比情况,从而帮助了解用户使用的设备分布情况。

下面是一个简单的示例代码,展示如何使用Node.js来抓取微博评论并进行设备分析:

const axios = require('axios');
const cheerio = require('cheerio');

async function fetchComments(url) {
    const response = await axios.get(url);
    const $ = cheerio.load(response.data);
    const comments = [];
    
    // 假设评论都在class为'.comment'的元素中
    $('.comment').each((index, element) => {
        const commentText = $(element).text();
        const deviceInfo = $(element).attr('data-device-info');
        
        if (deviceInfo) {
            comments.push({
                text: commentText,
                device: parseDeviceInfo(deviceInfo)
            });
        }
    });

    return comments;
}

function parseDeviceInfo(info) {
    // 解析设备信息的逻辑
    return info.split(',').reduce((acc, item) => {
        const [key, value] = item.split(':');
        acc[key.trim()] = value.trim();
        return acc;
    }, {});
}

// 使用示例
fetchComments('https://weibo.com/example')
    .then(comments => {
        console.log('Comments:', comments);

        // 设备类型统计
        const deviceStats = {};
        comments.forEach(comment => {
            const deviceType = comment.device['Device Type'];
            if (deviceType in deviceStats) {
                deviceStats[deviceType]++;
            } else {
                deviceStats[deviceType] = 1;
            }
        });

        console.log('Device Stats:', deviceStats);
    })
    .catch(error => {
        console.error('Error fetching comments:', error);
    });

以上代码使用axios库获取网页内容,然后使用cheerio解析HTML以提取评论文本和设备信息。之后,代码将设备信息解析成JSON对象,并统计不同设备类型的数量。请注意,实际的HTML结构可能会有所不同,需要根据实际情况调整选择器和解析逻辑。

回到顶部