何爷 发表于 2024-5-30 15:38:16

【爬虫软件】用Python开发的小红书详情批量采集工具,含笔记正文、转评赞藏

一、背景介绍

1.1 爬取目标


用python开发的爬虫采集软件,可自动按笔记链接抓取笔记的详情数据。
为什么有了源码还开发界面软件呢?方便不懂编程代码的小白用户使用,无需安装python,无需改代码,双击打开即用!
软件界面截图:

爬取结果截图:
结果截图1:

结果截图2:

结果截图3:

以上。
1.2 演示视频

软件使用演示:见原文
1.3 软件说明

几点重要说明:
以上。
二、代码讲解

2.1 爬虫采集模块

首先,定义接口地址作为请求地址:
# 请求地址
url = 'https://edith.xiaohongshu.com/api/sns/web/v1/feed'定义一个请求头,用于伪造浏览器:
# 请求头
h1 = {
        'Accept': 'application/json, text/plain, */*',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        'Content-Type': 'application/json;charset=UTF-8',
        'Cookie': '换成自己的cookie值',
        'Origin': 'https://www.xiaohongshu.com',
        'Referer': 'https://www.xiaohongshu.com/',
        'Sec-Ch-Ua': '"Microsoft Edge";v="119", "Chromium";v="119", "Not?A_Brand";v="24"',
        'Sec-Ch-Ua-Mobile': '?0',
        'Sec-Ch-Ua-Platform': '"macOS"',
        'Sec-Fetch-Dest': 'empty',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Site': 'same-site',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0',
}说明一下,cookie是个关键参数。
其中,cookie里的a1和web_session获取方法,如下:

这两个值非常重要,软件界面需要填写!!
加上请求参数,告诉程序你的爬取条件是什么:
# 请求参数
post_data = {
        "source_note_id": note_id,
        "image_formats": ["jpg", "webp", "avif"],
        "extra": {"need_body_topic": "1"}
}下面就是发送请求和接收数据:
# 发送请求
r = requests.post(url, headers=h1, data=data_json)
# 接收数据
json_data = r.json()逐个解析字段数据,以"笔记标题"为例:
# 笔记标题
try:
        title = json_data['data']['items']['note_card']['title']
except:
        title = ''熟悉xhs的朋友都知道,有些笔记是没有标题的,所以这里加上try保护,防止程序报错导致中断运行。
其他字段同理,不再赘述。
最后,是把数据保存到csv文件:
# 返回数据
data_row = note_id, title, desc, create_time, update_time, ip_location, like_count, collected_count, comment_count, share_count, nickname, user_id, user_url
# 保存到csv文件
with open(self.result_file, 'a+', encoding='utf_8_sig', newline='') as f:
        writer = csv.writer(f)
        writer.writerow(data_row)这里采用csv库保存数据,方便每爬取一条笔记数据,快速保存到csv文件中。
完整代码中,还含有:判断循环结束条件、转换时间戳、js逆向解密等关键实现逻辑,详见文末。
2.2 软件界面模块

主窗口部分:
# 创建主窗口
root = tk.Tk()
root.title('小红书详情采集软件v1 | 马哥python说 |')
# 设置窗口大小
root.minsize(width=850, height=650)输入控件部分:
# a1填写
tk.Label(root, justify='left', text='a1:').place(x=30, y=80)
entry_a1 = tk.Text(root, bg='#ffffff', width=96, height=2, )
entry_a1.place(x=125, y=80, anchor='nw')# 摆放位置
# web_session填写
tk.Label(root, justify='left', text='web_session:').place(x=30, y=120)
entry_web_session = tk.Text(root, bg='#ffffff', width=96, height=2, )
entry_web_session.place(x=125, y=120, anchor='nw')# 摆放位置底部版权部分:
# 版权信息
copyright = tk.Label(root, text='@马哥python说 All rights reserved.', font=('仿宋', 10), fg='grey')
copyright.place(x=290, y=625)以上。
2.3 日志模块

好的日志功能,方便软件运行出问题后快速定位原因,修复bug。
核心代码:
def get_logger(self):
        self.logger = logging.getLogger(__name__)
        # 日志格式
        formatter = '[%(asctime)s-%(filename)s][%(funcName)s-%(lineno)d]--%(message)s'
        # 日志级别
        self.logger.setLevel(logging.DEBUG)
        # 控制台日志
        sh = logging.StreamHandler()
        log_formatter = logging.Formatter(formatter, datefmt='%Y-%m-%d %H:%M:%S')
        # info日志文件名
        info_file_name = time.strftime("%Y-%m-%d") + '.log'
        # 将其保存到特定目录
        case_dir = r'./logs/'
        info_handler = TimedRotatingFileHandler(filename=case_dir + info_file_name,
                                                                                when='MIDNIGHT',
                                                                                interval=1,
                                                                                backupCount=7,
                                                                                encoding='utf-8')日志文件截图:

以上。
三、转载声明

转载已获原作者 @马哥python说 授权:
博客园原文链接: 【GUI软件】小红书详情数据批量采集,含笔记内容、转评赞藏等,支持多笔记同时采集!
持续分享Python干货中,欢迎交流开发技术!

来源:https://www.cnblogs.com/ws235/p/18222489
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: 【爬虫软件】用Python开发的小红书详情批量采集工具,含笔记正文、转评赞藏