当前位置: 首页 > 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.lryc.cn/news/622431.html

相关文章:

  • 补充日志之-配置文件解析指南(Centos7)
  • CAXA电子图板2026(国产CAD之光)
  • 机器学习之PCA
  • pyqt5无法显示opencv绘制文本和掩码信息
  • OpenCV 阈值处理
  • 应用侧华为云LoTDA设备接入平台
  • 重塑工业设备制造格局:明远智睿 T113-i 的破局之道
  • IgH初始化--未接从站
  • 【160页PPT】机械行业数字化生产供应链产品解决方案(附下载方式)
  • 光伏工单智能管理,故障处理快人一步
  • 状态流程框架(cola-component-statemachine)
  • WinForm 简单用户登录记录器实现教程
  • 五、ZooKeeper、Kafka、Hadoop、HBase、Spark、Flink集群化软件的部署
  • Sui 主网升级至 V1.53.2
  • 【DDIA】第三部分:衍生数据
  • 俄罗斯信封套娃问题-二维最长递增子序列
  • day29-进程和线程(2)
  • python自学笔记9 Seaborn可视化
  • 47.分布式事务理论
  • Elasticsearch:使用 Gradio 来创建一个简单的 RAG 应用界面
  • LeetCode刷题记录----236.二叉树的最近公共节点(medium)
  • 终极手撸cpu系列-详解底层原理-CPU硬核解剖:从0和1到 看透CPU逻辑设计内部原理,弄清楚现代多线程cpu工作原理
  • IC(Integrated Circuit,集成电路)是什么?
  • Qt——常用Widget(控件)
  • 数据结构初阶(17)排序算法——非比较排序、排序算法总结
  • Git、JSON、MQTT
  • 【Javaweb学习|黑马笔记|Day1】初识,入门网页,HTML-CSS|常见的标签和样式|标题排版和样式、正文排版和样式
  • 混凝土抗压强度预测:基于机器学习的全流程实战解析​
  • flume实战:从零配置到启动运行的完整指南
  • 【嵌入式C语言】五