nodejs爬虫小红书评论区

发现好像还是爬虫的知识热度比较高,最近一直在加强JS这块。这两天脚本模拟爬BOSS的时候也想着怎么用nodejs,昨天都没更新文章,Q-Q,因为一直failed没啥成果。

使用模块

这边可以看到使用的模块其实也挺多,但主要还是http和https模块,这两个用来爬取信息算是比较方便的

var http=require('http');
var https=require('https');
var _=require('lodash')
const { createObjectCsvWriter } = require('csv-writer');
const download=require('download')
const csvWriter = createObjectCsvWriter({
    path: 'comments.csv',
    header: [
        { id: 'comment_id', title: 'Comment ID' },
        { id: 'user_id', title: 'User ID' },
        { id: 'content', title: 'Content' },
        { id: 'url', title: 'url' },
        // { id: 'parent_id', title: 'Parent Comment ID' }  // 添加父评论
    ]
});

先是生成了一个csv文件用于存储到时候爬取到的信息

    const options = {
        hostname: 'edith.xiaohongshu.com',
        port: 443,
        path: '/api/sns/web/v2/comment/page?note_id=6663171e000000000d00f19c&cursor=&top_comment_id=&image_formats=jpg,webp,avif',
        method: 'GET',
        headers: {
            'Cookie':'你的cookie',
            'User-Agent': 'User-Agent'  // 可选: 设置User-Agent
        }
    };

这边主要就是http请求的信息设定了,headers信息直接复制就好了

调用HTTPS请求并将结果存放到data

    https.get(options2,(resp)=>{
         let data=''
         resp.on("data",(chunk)=>{
            data+=chunk;
        });
        resp.on('end',()=>{

得到的data会是很长的内容,需要用正则表达式将数据分割出来

        resp.on('end',()=>{
            // <script[^>]*>\s*window\.__INITIAL_STATE__\s*=\s*(\{[\s\S]*?\})
            const scriptRegex = /{"id":"([^"]+)",("modelType":"[^"]+")/g;
            let match;
            
            while ((match = scriptRegex.exec(data)) !== null) {
                if (match[1]) {
                    const initialState = match[1];

再将需要的数据存放到csv文件中

                            csvWriter.writeRecords(records, { append: true })
                            .then(() => {
                                console.log('The CSV file was written successfully');
                            })
                            .catch(err => {
                                console.error('Error writing CSV file:', err);
                            }); 

相关推荐

  1. Golang Colly下载详情页面图片爬虫

    2024-06-18 14:50:02       13 阅读
  2. 获得笔记详情 API

    2024-06-18 14:50:02       51 阅读
  3. 运营教程

    2024-06-18 14:50:02       5 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-18 14:50:02       10 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-18 14:50:02       12 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-18 14:50:02       11 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-18 14:50:02       14 阅读

热门阅读

  1. PHP中的for循环:多方面探讨与实际应用

    2024-06-18 14:50:02       7 阅读
  2. Qt 插件框架

    2024-06-18 14:50:02       6 阅读
  3. 力扣1385.两个数组间的距离值

    2024-06-18 14:50:02       6 阅读
  4. 【Python高级编程】使用OpenCV进行图像旋转详解

    2024-06-18 14:50:02       6 阅读
  5. CSS行内样式书写规范及注意事项

    2024-06-18 14:50:02       4 阅读
  6. 查看mysql数据库端口号

    2024-06-18 14:50:02       6 阅读
  7. 美股 — “四巫日”

    2024-06-18 14:50:02       7 阅读
  8. MyBatis 插件机制详解

    2024-06-18 14:50:02       8 阅读
  9. 富格林:可信守则有效防范暗箱

    2024-06-18 14:50:02       7 阅读
  10. 2024.6.17总结1113

    2024-06-18 14:50:02       7 阅读
  11. AI之Lambda index

    2024-06-18 14:50:02       7 阅读
  12. python中的结构

    2024-06-18 14:50:02       6 阅读
  13. 山东大学软件学院深度学习期末回忆版

    2024-06-18 14:50:02       8 阅读
  14. C# Socket通讯简单Demo

    2024-06-18 14:50:02       6 阅读