首页 > 大数据 > 数据分析 > 小红书笔记评论一键获取,实时查看作品数据
在小红书中,若想一键获取笔记评论并实时查看作品数据,可通过官方API、第三方工具及非官方技术方案实现,以下是一些具体的方法和工具推荐:
步骤:
https://api.xiaohongshu.com/v2/notes/{note_id}/comments,来获取指定笔记的评论数据。参数说明:
note_id:笔记的唯一标识符。page:分页页码,默认为1。page_size:每页返回评论数,默认为10。timestamp:请求时间戳,用于签名验证。sign:签名,通过app_id、app_secret、时间戳及参数生成MD5加密。Python示例代码:
pythonimport requestsimport hashlibimport timedef get_comments(note_id, access_token, page=1, page_size=10):url = f"https://api.xiaohongshu.com/v2/notes/{note_id}/comments"timestamp = str(int(time.time()))# 生成签名(示例,需替换为实际app_id和app_secret)sign_str = "your_app_id" + note_id + str(page) + str(page_size) + timestamp + "your_app_secret"sign = hashlib.md5(sign_str.encode('utf-8')).hexdigest()params = {"access_token": access_token,"page": page,"limit": page_size,"timestamp": timestamp,"sign": sign}response = requests.get(url, params=params)return response.json() if response.status_code == 200 else None# 示例调用note_id = "649c46ab000000002702ad36"access_token = "your_access_token"comments_data = get_comments(note_id, access_token)if comments_data:for comment in comments_data["data"]["comments"]:print(f"评论内容: {comment['content']}")print(f"用户昵称: {comment['user_nickname']}")print(f"点赞数: {comment['like_count']}")
pythonimport requestsfrom urllib.parse import urlencodeimport timeclass XiaohongshuCommentScraper:def __init__(self):self.headers = {"User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 16_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148","X-Request-ID": "api.comment_list","Content-Type": "application/json"}self.base_url = "https://www.xiaohongshu.com/api/sns/note/v1/comment_list"def get_comments(self, note_id, max_pages=5):comments = []for page in range(1, max_pages+1):params = {"note_id": note_id,"sort": "newest","page": page,"page_size": 20}try:response = requests.get(self.base_url, headers=self.headers, params=urlencode(params))data = response.json()if data.get('errcode') != 0:breakcomments.extend([{"id": c["id"],"user": c["user"]["nickname"],"content": c["content"],"like_count": c["like_count"],"create_time": c["create_time"]} for c in data.get('data', {}).get('comments', [])])time.sleep(1.5) # 反爬延迟except Exception as e:print(f"Error on page {page}: {str(e)}")breakreturn comments# 示例调用scraper = XiaohongshuCommentScraper()note_id = "649c46ab000000002702ad36"comments = scraper.get_comments(note_id, max_pages=3)print(f"成功抓取{len(comments)}条评论")
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70046834/viewspace-3095681/ ,如需转载,请注明出处,否则将追究法律责任。