当前位置: 首页 > news >正文

小红书帖子评论的nodejs爬虫脚本

从小红书上爬取评论,但是目前还不能完全爬取子评论,使用GPT没能解决这个问题。

后续博主可能会改进。或者如果你懂的话,可以在博主代码基础上改进。

需要安装nodejs软件,部署环境变量。博主是在pycharm中运行的。

代码无套路获取。自行修改参数(中文在代码里标记了)即可。

var http = require('http');
var https = require('https');
var _ = require('lodash');
const XLSX = require('xlsx');  // 引入 xlsx 库
const path = require('path');// API 请求配置
const options = {hostname: 'edith.xiaohongshu.com',port: 443,path: '/api/sns/web/v2/comment/page?note_id=你要爬取的笔记id&cursor=&top_comment_id=&image_formats=jpg,webp,avif',method: 'GET',headers: {'Cookie': '你的cookie'}
};https.get(options, (resp) => {let data = '';resp.on("data", (chunk) => {data += chunk;});resp.on('end', () => {console.log('Response Data:', data);  // Print the raw responsetry {const jsonResponse = JSON.parse(data);// Check if the response contains the expected data structureif (jsonResponse.data && jsonResponse.data.comments) {const records = [];let commentIdCounter = 1; // Initialize a counter for parent comment IDs// Process parent commentsjsonResponse.data.comments.forEach(item => {const parentComment = {comment_id: commentIdCounter++,  // Assign unique ID for parent commentsnickname: item.user_info && item.user_info.nickname ? item.user_info.nickname : 'No Nickname',content: item.content || '',url: item.pictures?.[0]?.url || '', // First image URLparent_comment_id: 'Parent Comment', // Mark parent comments as 'Parent Comment'};records.push(parentComment);// Process sub-comments and add indentation to show hierarchyif (item.sub_comments && item.sub_comments.length > 0) {item.sub_comments.forEach(subItem => {const subComment = {comment_id: commentIdCounter++,  // Assign unique ID for sub-commentsnickname: subItem.user_info && subItem.user_info.nickname ? subItem.user_info.nickname : 'No Nickname',content: '    ' + (subItem.content || ''), // Indent to show it's a sub-commenturl: subItem.pictures?.[0]?.url || '', // First image URLparent_comment_id: parentComment.comment_id // Link sub-comment to parent comment};records.push(subComment);});}});// Sort records by the original order (comment_id) or creation timerecords.sort((a, b) => a.comment_id - b.comment_id);// Create a new workbook and add a sheetconst wb = XLSX.utils.book_new();const ws = XLSX.utils.json_to_sheet(records);// Add the sheet to the workbookXLSX.utils.book_append_sheet(wb, ws, 'Comments');// Save the workbook as an XLSX fileconst filePath = path.join(__dirname, 'comments_with_parent_child_hierarchy.xlsx');XLSX.writeFile(wb, filePath);console.log('The XLSX file was written successfully at:', filePath);} else {console.error('No comments data found or data structure is incorrect');}} catch (error) {console.error('Error parsing response data:', error);}});}).on('error', (err) => {console.error('Request failed:', err);
});

http://www.dtcms.com/a/333813.html

相关文章:

  • C++编程学习(第24天)
  • 数据结构与算法p4
  • Eclipse:关闭项目
  • 【121页PPT】锂膜产业MESERP方案规划建议(附下载方式)
  • Git、JSON、MQTT
  • ramdisk内存虚拟盘(一)——前世今生
  • 嵌入式第二十九课!!!回收子进程资源空间函数与exec函数
  • SurperSet柱状图排序失效问题解决
  • 移动板房的网络化建设
  • python中的reduce函数
  • FTP定时推拉数据思考
  • 深入理解 Python 闭包:从原理到实践
  • AI - MCP 协议(一)
  • NY232NY236美光固态闪存NY240NY241
  • Dummy步进电机驱动使用和相关问题
  • 疏老师-python训练营-Day46通道注意力(SE注意力)
  • 高通vendor app访问文件
  • 【使用三化总结大模型基础概念】
  • 淘宝/天猫店铺商品搜索利器:taobao.item_search_shop API返回值详解
  • 【秋招笔试】2025.08.15饿了么秋招机考-第一题
  • 嵌入式linux学习 -- 进程和线程
  • CIAIE 2025上海汽车内外饰展观察:从美学到功能的产业跃迁
  • Redis 启动时出现 “Bad file format reading the append only file“ 错误
  • 【万字精讲】 左枝清减·右枝丰盈:C++构筑的二叉搜索森林
  • office2016常见故障解决方法
  • 第七十一章:AI的“个性定制服务”:微调 LLM vs 微调 Diffusion 模型——谁是“魔改之王”?
  • 展览讯息易天邀您共赴第26届中国国际光电博览会
  • AI创业公司分析:Paloma
  • 网络通讯核心知识
  • AI的拜师学艺,模型蒸馏技术