
下面给你一套最简单、直接能用的小红书评论数据可视化方案,用 Python + 通用库 matplotlib / pyecharts 实现,不需要复杂配置。
bash
运行
pip install requests matplotlib pandaspython
运行
import requests
import matplotlib.pyplot as plt
import time
# ===================== 配置 =====================
ACCESS_TOKEN = "你的token"
NOTE_ID = "笔记ID"
# =================================================
def get_xhs_comments(note_id, cursor=""):
"""获取评论"""
url = f"https://api.xiaohongshu.com/v2/notes/{note_id}/comments"
headers = {"Authorization": f"Bearer {ACCESS_TOKEN}"}
params = {"cursor": cursor, "page_size": 50, "sort": "time"}
resp = requests.get(url, headers=headers, params=params)
return resp.json()
def parse_comments(json_data):
"""解析评论数据"""
comments = json_data.get("data", {}).get("comments", [])
result = []
for c in comments:
result.append({
"like": c.get("like_count", 0),
"time": c.get("create_time", 0),
"content_len": len(c.get("content", "")),
"username": c.get("user", {}).get("nickname", "")
})
return result
# 获取并解析
data = get_xhs_comments(NOTE_ID)
comment_list = parse_comments(data)
# 提取数据用于画图
likes = [c["like"] for c in comment_list]
times = [c["time"] for c in comment_list]
lengths = [c["content_len"] for c in comment_list]
# ===================== 可视化 =====================
plt.rcParams["font.sans-serif"] = ["SimHei"] # 显示中文
plt.rcParams["axes.unicode_minus"] = False
# 1. 点赞数分布柱状图
plt.figure(figsize=(12,5))
plt.subplot(1,3,1)
plt.bar(range(len(likes)), likes, color="#ff5c5c")
plt.title("评论点赞数")
plt.xlabel("评论序号")
plt.ylabel("点赞数")
# 2. 评论长度分布
plt.subplot(1,3,2)
plt.hist(lengths, bins=10, color="#5ac8fa")
plt.title("评论长度分布")
plt.xlabel("字数")
plt.ylabel("条数")
# 3. 评论时间折线(时间戳转时间)
time_str = [time.strftime("%m-%d %H:%M", time.localtime(t)) for t in times]
plt.subplot(1,3,3)
plt.plot(time_str, likes, marker="o", color="#34c759")
plt.title("点赞-时间趋势")
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()如果你想看评论关键词云,可以用:
bash
运行
pip install wordcloud jiebapython
运行
from wordcloud import WordCloud
import jieba
# 拼接所有评论内容
contents = [c.get("content", "") for c in json_data["data"]["comments"]]
text = " ".join(contents)
words = " ".join(jieba.lcut(text))
wc = WordCloud(
font_path="msyh.ttc", # Windows
# font_path="PingFang.ttc" # Mac
width=800, height=400, background_color="white"
)
wc.generate(words)
wc.to_file("xhs_wordcloud.png")
plt.imshow(wc)
plt.axis("off")
plt.show()原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。