小红书数据采集终极指南:Python实战与完整解决方案

news2026/5/2 11:47:28
小红书数据采集终极指南Python实战与完整解决方案【免费下载链接】xhs基于小红书 Web 端进行的请求封装。https://reajason.github.io/xhs/项目地址: https://gitcode.com/gh_mirrors/xh/xhs在小红书内容运营和数据分析领域获取高质量数据是优化策略的关键。xhs是一个基于小红书Web端请求封装的Python库为开发者提供了高效、稳定的数据采集能力。本文将深度解析如何利用xhs库进行小红书数据采集涵盖从环境搭建到实战应用的完整流程帮助您构建专业的数据采集系统。 核心功能概览xhs库能做什么xhs库提供了丰富的小红书数据接口主要功能包括笔记数据获取获取笔记详情、评论、点赞等互动数据用户信息采集获取用户资料、发布笔记、收藏和点赞记录搜索功能支持关键词搜索、话题搜索等创作者中心获取创作者相关的数据统计反爬虫处理内置签名机制应对平台反爬策略 技术架构对比功能维度xhs库方案传统爬虫方案官方API方案开发成本中等Python库高需从头开发低但申请复杂稳定性高持续维护低易被封禁最高数据完整性高覆盖主要接口视开发能力而定有限有调用限制合规性中等需注意频率低易违规最高更新频率及时社区维护需自行维护官方更新 快速开始环境搭建与基础使用安装与配置首先安装xhs库及其依赖# 安装xhs库 pip install xhs # 安装playwright用于签名 pip install playwright playwright install # 下载反检测脚本 curl -O https://cdn.jsdelivr.net/gh/requireCool/stealth.min.js/stealth.min.js基础示例获取笔记详情from xhs import XhsClient, help import datetime import json # 初始化客户端需要有效的cookie cookie your_cookie_here xhs_client XhsClient(cookie) # 获取笔记详情 note_id 6505318c000000001f03c5a6 xsec_token your_xsec_token note xhs_client.get_note_by_id(note_id, xsec_token) # 提取图片URL image_urls help.get_imgs_url_from_note(note) print(f笔记标题{note.get(title)}) print(f图片数量{len(image_urls)})️ 签名机制应对反爬的核心技术小红书采用了复杂的签名机制来防止自动化请求xhs库通过playwright模拟浏览器环境来获取有效签名。签名服务部署方案对于生产环境建议部署独立的签名服务# 签名服务端示例 from flask import Flask, request from playwright.sync_api import sync_playwright import time app Flask(__name__) def init_browser(): playwright sync_playwright().start() browser playwright.chromium.launch(headlessTrue) context browser.new_context() page context.new_page() page.goto(https://www.xiaohongshu.com) return page app.route(/sign, methods[POST]) def sign_endpoint(): data request.json # 调用浏览器环境进行签名 result page.evaluate(([url, data]) window._webmsxyw(url, data), [data[uri], data[data]]) return {x-s: result[X-s], x-t: str(result[X-t])}多账号签名管理# 多账号签名管理策略 class SignManager: def __init__(self): self.sign_services { account1: http://localhost:5001/sign, account2: http://localhost:5002/sign } def get_signature(self, account, uri, data): # 轮询使用不同签名服务 service_url self.sign_services[account] response requests.post(service_url, json{uri: uri, data: data}) return response.json() 实战应用构建完整的数据采集系统1. 用户数据采集系统class UserDataCollector: def __init__(self, xhs_client): self.client xhs_client def collect_user_profile(self, user_id): 采集用户基本信息 profile self.client.get_user_info(user_id) return { user_id: user_id, nickname: profile.get(nickname), fans_count: profile.get(fans_count), notes_count: profile.get(notes_count), likes_count: profile.get(likes_count) } def collect_user_notes(self, user_id, limit100): 采集用户发布的所有笔记 all_notes [] cursor while len(all_notes) limit: notes_data self.client.get_user_notes(user_id, cursorcursor) notes notes_data.get(notes, []) all_notes.extend(notes) if not notes_data.get(has_more, False): break cursor notes_data.get(cursor, ) # 控制请求频率 time.sleep(1) return all_notes[:limit]2. 竞品监控系统class CompetitorMonitor: def __init__(self, competitor_ids): self.competitors competitor_ids self.data_store {} def daily_monitoring(self): 每日竞品数据监控 daily_report {} for competitor_id in self.competitors: # 获取基础数据 profile self.client.get_user_info(competitor_id) notes self.client.get_user_all_notes(competitor_id) # 计算关键指标 metrics self.calculate_metrics(notes) daily_report[competitor_id] { profile: profile, metrics: metrics, timestamp: datetime.now().isoformat() } # 存储到数据库 self.save_to_database(competitor_id, daily_report[competitor_id]) return daily_report def calculate_metrics(self, notes): 计算互动指标 total_likes sum(note.get(likes, 0) for note in notes) total_collects sum(note.get(collects, 0) for note in notes) total_comments sum(note.get(comments, 0) for note in notes) return { avg_likes: total_likes / len(notes) if notes else 0, avg_collects: total_collects / len(notes) if notes else 0, avg_comments: total_comments / len(notes) if notes else 0, engagement_rate: (total_likes total_collects total_comments) / len(notes) if notes else 0 }3. 内容趋势分析系统class ContentTrendAnalyzer: def analyze_trends_by_keyword(self, keyword, days7): 分析关键词趋势 trends_data [] for i in range(days): date datetime.now() - timedelta(daysi) notes self.client.get_note_by_keyword( keywordkeyword, sortgeneral, page1 ) daily_stats { date: date.strftime(%Y-%m-%d), total_notes: len(notes), avg_likes: self.calculate_avg(notes, likes), avg_comments: self.calculate_avg(notes, comments), hot_topics: self.extract_topics(notes) } trends_data.append(daily_stats) return trends_data def extract_topics(self, notes): 提取热门话题标签 from collections import Counter all_tags [] for note in notes: tags note.get(tag_list, []) all_tags.extend([tag.get(name) for tag in tags if tag.get(name)]) return Counter(all_tags).most_common(10) 高级配置与优化策略请求频率控制class RateLimitedClient: def __init__(self, xhs_client, requests_per_minute30): self.client xhs_client self.rate_limit requests_per_minute self.request_times [] def make_request(self, method, *args, **kwargs): # 控制请求频率 self.wait_if_needed() # 记录请求时间 self.request_times.append(time.time()) # 清理过期记录 current_time time.time() self.request_times [t for t in self.request_times if current_time - t 60] return method(*args, **kwargs) def wait_if_needed(self): if len(self.request_times) self.rate_limit: oldest_time self.request_times[0] wait_time 60 - (time.time() - oldest_time) if wait_time 0: time.sleep(wait_time)错误处理与重试机制class ResilientDataCollector: def __init__(self, max_retries3, backoff_factor2): self.max_retries max_retries self.backoff_factor backoff_factor def collect_with_retry(self, collect_func, *args, **kwargs): 带重试机制的数据采集 for attempt in range(self.max_retries): try: return collect_func(*args, **kwargs) except Exception as e: if attempt self.max_retries - 1: raise # 指数退避 wait_time self.backoff_factor ** attempt print(f请求失败{wait_time}秒后重试... 错误: {str(e)}) time.sleep(wait_time) 数据存储与处理流程数据存储方案import sqlite3 import json from datetime import datetime class DataStorage: def __init__(self, db_pathxhs_data.db): self.conn sqlite3.connect(db_path) self.create_tables() def create_tables(self): 创建数据表 tables [ CREATE TABLE IF NOT EXISTS notes ( note_id TEXT PRIMARY KEY, user_id TEXT, title TEXT, content TEXT, likes INTEGER, collects INTEGER, comments INTEGER, create_time TIMESTAMP, raw_data TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ) , CREATE TABLE IF NOT EXISTS users ( user_id TEXT PRIMARY KEY, nickname TEXT, fans_count INTEGER, notes_count INTEGER, collected_at TIMESTAMP ) ] for table_sql in tables: self.conn.execute(table_sql) self.conn.commit() def save_note(self, note_data): 保存笔记数据 sql INSERT OR REPLACE INTO notes (note_id, user_id, title, content, likes, collects, comments, create_time, raw_data) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) self.conn.execute(sql, ( note_data.get(id), note_data.get(user_id), note_data.get(title), note_data.get(desc), note_data.get(likes, 0), note_data.get(collects, 0), note_data.get(comments, 0), datetime.fromtimestamp(note_data.get(time, 0)), json.dumps(note_data, ensure_asciiFalse) )) self.conn.commit()数据清洗与标准化class DataProcessor: def clean_note_data(self, raw_note): 清洗笔记数据 cleaned { note_id: raw_note.get(id), title: self.clean_text(raw_note.get(title, )), content: self.clean_text(raw_note.get(desc, )), user_info: self.extract_user_info(raw_note), interaction: self.calculate_interaction_score(raw_note), tags: self.extract_tags(raw_note), media_info: self.extract_media_info(raw_note) } return cleaned def clean_text(self, text): 清理文本内容 import re # 移除多余空格和换行 text re.sub(r\s, , text).strip() # 移除特殊字符但保留中文和基本标点 text re.sub(r[^\w\u4e00-\u9fff\s.,!?;:。], , text) return text 合规指南与最佳实践合规采集原则尊重robots协议遵守小红书的robots.txt规定控制请求频率建议每秒不超过1个请求每日总量控制在合理范围避免隐私数据不采集用户手机号、邮箱等隐私信息明确使用目的数据仅用于个人学习或研究分析设置User-Agent使用合理的User-Agent标识风险规避策略class SafeCollector: def __init__(self): self.request_count 0 self.last_request_time time.time() def safe_request(self, request_func): 安全请求包装器 # 检查请求频率 current_time time.time() if current_time - self.last_request_time 1.0: time.sleep(1.0 - (current_time - self.last_request_time)) # 限制每日请求量 if self.request_count 10000: print(已达到每日请求限制暂停采集) return None try: result request_func() self.request_count 1 self.last_request_time time.time() return result except Exception as e: print(f请求失败: {str(e)}) # 遇到错误时增加等待时间 time.sleep(5) return None 性能优化建议1. 并发处理优化import concurrent.futures from typing import List class ConcurrentCollector: def collect_multiple_notes(self, note_ids: List[str], max_workers5): 并发采集多个笔记 results {} with concurrent.futures.ThreadPoolExecutor(max_workersmax_workers) as executor: future_to_note { executor.submit(self.client.get_note_by_id, note_id, xsec_token): note_id for note_id in note_ids } for future in concurrent.futures.as_completed(future_to_note): note_id future_to_note[future] try: results[note_id] future.result() except Exception as e: print(f采集笔记 {note_id} 失败: {str(e)}) results[note_id] None return results2. 缓存策略import hashlib import pickle from functools import lru_cache class CachedClient: def __init__(self, cache_dir.cache): self.cache_dir cache_dir os.makedirs(cache_dir, exist_okTrue) lru_cache(maxsize1000) def get_note_with_cache(self, note_id, xsec_token): 带缓存的笔记获取 cache_key self.generate_cache_key(note, note_id) cache_file os.path.join(self.cache_dir, cache_key) # 检查缓存是否存在且未过期1小时 if os.path.exists(cache_file): file_age time.time() - os.path.getmtime(cache_file) if file_age 3600: # 1小时缓存 with open(cache_file, rb) as f: return pickle.load(f) # 获取新数据 note self.client.get_note_by_id(note_id, xsec_token) # 保存到缓存 with open(cache_file, wb) as f: pickle.dump(note, f) return note def generate_cache_key(self, data_type, identifier): 生成缓存键 return hashlib.md5(f{data_type}_{identifier}.encode()).hexdigest() 实际应用场景示例场景一内容运营分析class ContentOperationAnalyzer: def analyze_content_performance(self, user_id, days30): 分析内容运营效果 notes self.get_recent_notes(user_id, days) analysis { total_notes: len(notes), total_interactions: self.sum_interactions(notes), avg_engagement_rate: self.calculate_engagement_rate(notes), best_performing_notes: self.get_top_notes(notes, limit5), content_categories: self.analyze_content_categories(notes), publishing_pattern: self.analyze_publishing_pattern(notes) } return analysis def get_recent_notes(self, user_id, days): 获取最近发布的笔记 all_notes self.client.get_user_all_notes(user_id) cutoff_date datetime.now() - timedelta(daysdays) recent_notes [] for note in all_notes: note_time datetime.fromtimestamp(note.get(time, 0)) if note_time cutoff_date: recent_notes.append(note) return recent_notes场景二竞品对标分析class CompetitorBenchmark: def benchmark_against_competitors(self, main_account, competitors): 与竞品对标分析 benchmark_data {} # 获取主账号数据 main_data self.get_account_metrics(main_account) benchmark_data[main_account] main_data # 获取竞品数据 for competitor in competitors: competitor_data self.get_account_metrics(competitor) benchmark_data[competitor] competitor_data # 计算差距 gap_analysis self.calculate_gap(main_data, competitor_data) benchmark_data[f{competitor}_gap] gap_analysis return benchmark_data def get_account_metrics(self, user_id): 获取账号核心指标 profile self.client.get_user_info(user_id) notes self.client.get_user_all_notes(user_id) return { fans_growth_rate: self.calculate_growth_rate(profile), content_engagement: self.calculate_engagement(notes), content_consistency: self.calculate_consistency(notes), top_content_performance: self.get_top_content(notes) } 总结与建议实施建议分阶段实施从小规模测试开始逐步扩大采集范围监控与调整实时监控采集效果及时调整策略数据质量优先确保数据准确性比数据量更重要合规性检查定期检查采集行为是否符合平台政策常见问题解决签名失败检查cookie有效性确保a1字段正确请求频率限制增加请求间隔使用多IP轮换数据不完整检查网络连接确认参数正确性性能问题优化代码结构使用缓存和并发处理通过本文介绍的完整方案您可以构建一个稳定、高效、合规的小红书数据采集系统。xhs库提供了强大的基础功能结合合理的架构设计和优化策略可以满足从个人研究到企业级应用的各种需求。核心要点回顾xhs库提供了完整的小红书数据接口签名机制是关键建议部署独立签名服务控制请求频率遵守平台规则数据清洗和存储是价值提取的关键环节结合实际业务需求设计采集策略通过合理使用xhs库您可以高效获取小红书数据为内容运营、竞品分析、市场研究等场景提供有力支持。【免费下载链接】xhs基于小红书 Web 端进行的请求封装。https://reajason.github.io/xhs/项目地址: https://gitcode.com/gh_mirrors/xh/xhs创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2574859.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

SpringBoot-17-MyBatis动态SQL标签之常用标签

文章目录 1 代码1.1 实体User.java1.2 接口UserMapper.java1.3 映射UserMapper.xml1.3.1 标签if1.3.2 标签if和where1.3.3 标签choose和when和otherwise1.4 UserController.java2 常用动态SQL标签2.1 标签set2.1.1 UserMapper.java2.1.2 UserMapper.xml2.1.3 UserController.ja…

wordpress后台更新后 前端没变化的解决方法

使用siteground主机的wordpress网站,会出现更新了网站内容和修改了php模板文件、js文件、css文件、图片文件后,网站没有变化的情况。 不熟悉siteground主机的新手,遇到这个问题,就很抓狂,明明是哪都没操作错误&#x…

网络编程(Modbus进阶)

思维导图 Modbus RTU(先学一点理论) 概念 Modbus RTU 是工业自动化领域 最广泛应用的串行通信协议,由 Modicon 公司(现施耐德电气)于 1979 年推出。它以 高效率、强健性、易实现的特点成为工业控制系统的通信标准。 包…

UE5 学习系列(二)用户操作界面及介绍

这篇博客是 UE5 学习系列博客的第二篇,在第一篇的基础上展开这篇内容。博客参考的 B 站视频资料和第一篇的链接如下: 【Note】:如果你已经完成安装等操作,可以只执行第一篇博客中 2. 新建一个空白游戏项目 章节操作,重…

IDEA运行Tomcat出现乱码问题解决汇总

最近正值期末周,有很多同学在写期末Java web作业时,运行tomcat出现乱码问题,经过多次解决与研究,我做了如下整理: 原因: IDEA本身编码与tomcat的编码与Windows编码不同导致,Windows 系统控制台…

利用最小二乘法找圆心和半径

#include <iostream> #include <vector> #include <cmath> #include <Eigen/Dense> // 需安装Eigen库用于矩阵运算 // 定义点结构 struct Point { double x, y; Point(double x_, double y_) : x(x_), y(y_) {} }; // 最小二乘法求圆心和半径 …

使用docker在3台服务器上搭建基于redis 6.x的一主两从三台均是哨兵模式

一、环境及版本说明 如果服务器已经安装了docker,则忽略此步骤,如果没有安装,则可以按照一下方式安装: 1. 在线安装(有互联网环境): 请看我这篇文章 传送阵>> 点我查看 2. 离线安装(内网环境):请看我这篇文章 传送阵>> 点我查看 说明&#xff1a;假设每台服务器已…

XML Group端口详解

在XML数据映射过程中&#xff0c;经常需要对数据进行分组聚合操作。例如&#xff0c;当处理包含多个物料明细的XML文件时&#xff0c;可能需要将相同物料号的明细归为一组&#xff0c;或对相同物料号的数量进行求和计算。传统实现方式通常需要编写脚本代码&#xff0c;增加了开…

LBE-LEX系列工业语音播放器|预警播报器|喇叭蜂鸣器的上位机配置操作说明

LBE-LEX系列工业语音播放器|预警播报器|喇叭蜂鸣器专为工业环境精心打造&#xff0c;完美适配AGV和无人叉车。同时&#xff0c;集成以太网与语音合成技术&#xff0c;为各类高级系统&#xff08;如MES、调度系统、库位管理、立库等&#xff09;提供高效便捷的语音交互体验。 L…

(LeetCode 每日一题) 3442. 奇偶频次间的最大差值 I (哈希、字符串)

题目&#xff1a;3442. 奇偶频次间的最大差值 I 思路 &#xff1a;哈希&#xff0c;时间复杂度0(n)。 用哈希表来记录每个字符串中字符的分布情况&#xff0c;哈希表这里用数组即可实现。 C版本&#xff1a; class Solution { public:int maxDifference(string s) {int a[26]…

【大模型RAG】拍照搜题技术架构速览:三层管道、两级检索、兜底大模型

摘要 拍照搜题系统采用“三层管道&#xff08;多模态 OCR → 语义检索 → 答案渲染&#xff09;、两级检索&#xff08;倒排 BM25 向量 HNSW&#xff09;并以大语言模型兜底”的整体框架&#xff1a; 多模态 OCR 层 将题目图片经过超分、去噪、倾斜校正后&#xff0c;分别用…

【Axure高保真原型】引导弹窗

今天和大家中分享引导弹窗的原型模板&#xff0c;载入页面后&#xff0c;会显示引导弹窗&#xff0c;适用于引导用户使用页面&#xff0c;点击完成后&#xff0c;会显示下一个引导弹窗&#xff0c;直至最后一个引导弹窗完成后进入首页。具体效果可以点击下方视频观看或打开下方…

接口测试中缓存处理策略

在接口测试中&#xff0c;缓存处理策略是一个关键环节&#xff0c;直接影响测试结果的准确性和可靠性。合理的缓存处理策略能够确保测试环境的一致性&#xff0c;避免因缓存数据导致的测试偏差。以下是接口测试中常见的缓存处理策略及其详细说明&#xff1a; 一、缓存处理的核…

龙虎榜——20250610

上证指数放量收阴线&#xff0c;个股多数下跌&#xff0c;盘中受消息影响大幅波动。 深证指数放量收阴线形成顶分型&#xff0c;指数短线有调整的需求&#xff0c;大概需要一两天。 2025年6月10日龙虎榜行业方向分析 1. 金融科技 代表标的&#xff1a;御银股份、雄帝科技 驱动…

观成科技:隐蔽隧道工具Ligolo-ng加密流量分析

1.工具介绍 Ligolo-ng是一款由go编写的高效隧道工具&#xff0c;该工具基于TUN接口实现其功能&#xff0c;利用反向TCP/TLS连接建立一条隐蔽的通信信道&#xff0c;支持使用Let’s Encrypt自动生成证书。Ligolo-ng的通信隐蔽性体现在其支持多种连接方式&#xff0c;适应复杂网…

铭豹扩展坞 USB转网口 突然无法识别解决方法

当 USB 转网口扩展坞在一台笔记本上无法识别,但在其他电脑上正常工作时,问题通常出在笔记本自身或其与扩展坞的兼容性上。以下是系统化的定位思路和排查步骤,帮助你快速找到故障原因: 背景: 一个M-pard(铭豹)扩展坞的网卡突然无法识别了,扩展出来的三个USB接口正常。…

未来机器人的大脑:如何用神经网络模拟器实现更智能的决策?

编辑&#xff1a;陈萍萍的公主一点人工一点智能 未来机器人的大脑&#xff1a;如何用神经网络模拟器实现更智能的决策&#xff1f;RWM通过双自回归机制有效解决了复合误差、部分可观测性和随机动力学等关键挑战&#xff0c;在不依赖领域特定归纳偏见的条件下实现了卓越的预测准…

Linux应用开发之网络套接字编程(实例篇)

服务端与客户端单连接 服务端代码 #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <arpa/inet.h> #include <pthread.h> …

华为云AI开发平台ModelArts

华为云ModelArts&#xff1a;重塑AI开发流程的“智能引擎”与“创新加速器”&#xff01; 在人工智能浪潮席卷全球的2025年&#xff0c;企业拥抱AI的意愿空前高涨&#xff0c;但技术门槛高、流程复杂、资源投入巨大的现实&#xff0c;却让许多创新构想止步于实验室。数据科学家…

深度学习在微纳光子学中的应用

深度学习在微纳光子学中的主要应用方向 深度学习与微纳光子学的结合主要集中在以下几个方向&#xff1a; 逆向设计 通过神经网络快速预测微纳结构的光学响应&#xff0c;替代传统耗时的数值模拟方法。例如设计超表面、光子晶体等结构。 特征提取与优化 从复杂的光学数据中自…