小红书关键词爬虫

标题

  • [1 统计要收集的关键词,制作一个文件夹](#1 统计要收集的关键词,制作一个文件夹)
  • [2 爬取每一页的内容](#2 爬取每一页的内容)
  • [3 爬取标题和内容](#3 爬取标题和内容)
  • [4 如果内容可以被查看,爬取评论内容](#4 如果内容可以被查看,爬取评论内容)
  • [5 将结果进行汇总,并且每个帖子保存为一个json文件,具体内容](#5 将结果进行汇总,并且每个帖子保存为一个json文件,具体内容)
  • [6 总结](#6 总结)

1 统计要收集的关键词,制作一个文件夹

例如,我要收集旅游相关的,就收集:

旅游、旅行、旅游攻略,这些词,做成一个txt文件。

用一个浏览器登录上小红书账号,然后记录写cookies,例如:

2 爬取每一页的内容

主要使用request,js模块,将爬取的内容保存为res,里面包含一页20条数据。

python 复制代码
info = re.sub(r'"page":".*?"', f'"page":"{page}"', info)
        ret = js.call('get_xs', api, info, cookies["a1"])
        headers['x-s'], headers['x-t'] = ret['X-s'], str(ret['X-t'])
        response = requests.post(search_url, headers=headers, cookies=cookies, data=info.encode('utf-8'))
        res = response.json()

3 爬取标题和内容

从每一个note里面解析出标题,内容等信息。

python 复制代码
result = {}
    result["title"] = data['note_card']['title']
    result["desc"] = data['note_card']['desc'].replace("\n", "").replace("\t", "")
    tags_temp = data['note_card']['tag_list']
    tags = []
    for tag in tags_temp:
        try:
            tags.append(tag['name'])
        except:
            pass
    result["tags"] = tags
    result["time"] = timestamp_to_str(data['note_card']['time'])

4 如果内容可以被查看,爬取评论内容

每个帖子里面的评论的单独的url,需要根据id号进行拼接,所以根据第3节获取的user-id,进行拼接,然后再用get进行访问,最后获得每条评论,注意有些帖子是不能被查看的,所以需要进行判断。

python 复制代码
note_id = url.split('/')[-1]
    comments_url = "https://edith.xiaohongshu.com/api/sns/web/v2/comment/page?note_id={}&image_scenes=FD_WM_WEBP,CRD_WM_WEBP".format(
        note_id)
    response = requests.get(comments_url, headers=headers, cookies=cookies)
    res = response.json()
    comments = []
    for line in res["data"]["comments"]:
        comment_str = line["content"]
        comments.append(comment_str)

5 将结果进行汇总,并且每个帖子保存为一个json文件,具体内容

包含:标题,具体内容,标题,创建时间,评论内容。每个关键词一个文件夹。

6 总结

详细代码私聊,注意本内容没有爬取图片,如果需要可以添加。

相关推荐
路由侠内网穿透1 天前
本地部署 GPS 跟踪系统 Traccar 并实现外部访问
运维·服务器·网络·windows·tcp/ip
研华嵌入式1 天前
如何在高通跃龙QCS6490 Arm架构上使用Windows 11 IoT企业版?
arm开发·windows·嵌入式硬件
带娃的IT创业者2 天前
Windows 平台上基于 MCP 构建“文心一言+彩云天气”服务实战
人工智能·windows·文心一言·mcp
csdn_aspnet2 天前
Windows Node.js 安装及环境配置详细教程
windows·node.js
摇滚侠2 天前
java语言中,list<String>转成字符串,逗号分割;List<Integer>转字符串,逗号分割
java·windows·list
Source.Liu2 天前
【Pywinauto库】12.2 pywinauto.element_info 后端内部实施模块
windows·python·自动化
Source.Liu2 天前
【Pywinauto库】12.1 pywinauto.backend 后端内部实施模块
开发语言·windows·python·自动化
私人珍藏库2 天前
[Windows] FileOptimizer v17.1.0_一款文件批量压缩工具
windows·批量压缩
掘根2 天前
【CMake】List
windows·microsoft·list
TToolss2 天前
删除文件夹里的网盘图标
windows