如何利用知乎热榜和 Python 追踪中国正在讨论的热门话题

如何利用知乎热榜和 Python 追踪中国正在讨论的热门话题

使用 Python 和 Apify 抓取知乎热门话题的分步指南。

在当今快节奏的世界中,紧跟热门话题至关重要。对于了解不同地区(例如中国)的公众舆论而言尤为如此。截至 2026 年 11 月 26 日,知乎热榜为深入了解中国人当下正在讨论的内容提供了宝贵视角。然而,获取这些数据可能有些棘手,因为它是中文的,并且没有官方的公开 API。不过别担心!在本文中,我们将逐步介绍如何使用 Python 抓取知乎热榜。

什么是知乎?

Zhihu (知乎) 是中国最大的问答平台。它类似于 Quora,但拥有独特的风格和用户群。知乎热榜 (Zhihu Hot List) 展示了在任意给定时刻讨论最多的热门问题。该榜单变动频繁,使其成为获取实时洞察的绝佳来源。

为什么要追踪知乎热榜?

追踪知乎热榜有助于你了解中国当前的争论热点与趋势。这对于营销人员、记者以及任何对中国文化和公众舆论感兴趣的人都大有裨益。通过分析这些数据,你可以深入了解哪些话题能够引起中国受众的共鸣。

面临的挑战

获取知乎热榜的主要挑战包括:

  • 内容为中文,这对非中文使用者来说可能是一个障碍。
  • 榜单每隔几分钟就会更新一次,难以持续追踪。
  • 没有官方的公开 API 可供便捷访问。

我们的解决方案

在本教程中,我们将使用 Python 抓取知乎热榜。我们将提取数据为英文,并提取如下有用信息:

  • 问题的排名
  • 标题和摘要(附带英文翻译)
  • 热度得分 (热度)
  • 回答数量
  • 问题 URL
  • 数据提取时间

准备工作

在开始编写代码之前,你需要完成以下准备:

步骤 1:创建 Apify 账号

  1. 前往 apify.com 并创建一个免费账号。免费方案提供的额度足以完成本教程。

步骤 2:获取你的 API Token

  1. 登录后,导航至 Settings → API Integrations (在 Apify Console 中)。
  2. 复制你的 API token 备用。

步骤 3:安装 Apify 客户端

  1. 打开终端并运行以下命令以安装 Apify 客户端:
    pip install "apify-client==3"

步骤 4:将你的 API Token 设置为环境变量

  1. 在终端中,将 API token 设置为环境变量:
    export APIFY_TOKEN="your_token_here"
    替换 your_token_here 为你之前复制的 token。

将热榜数据拉取到 Python 中

现在一切已准备就绪,让我们编写代码来抓取知乎热榜。

步骤 5:编写 Python 代码

这是一个拉取热榜的简单脚本:

import os
from apify_client import ApifyClient

client = ApifyClient(os.environ["APIFY_TOKEN"])

run = client.actor("neronbaez/zhihu-hot-list-scraper").call(run_input={
    "maxItemsPerPlatform": 10,      # top 10 questions of the hot list
    "translateToEnglish": True,     # adds titleEn / excerptEn
})

questions = list(client.dataset(run.default_dataset_id).iterate_items())

for q in questions:
    print(f'#{q["rank"]:>2} | {q["heat"]:>9,} heat | {q.get("answers", 0):>5} answers | '
          f'{q.get("titleEn") or q["title"]}')

步骤 6:运行代码

  1. 将脚本保存为 zhihu_scraper.py.
  2. 在终端中运行它:
    python zhihu_scraper.py

示例输出

运行该脚本时,你应该会看到类似如下的输出:

# 1 | 8,230,000 heat | 98 answers | The launch of the Shenzhou 24 manned spacecraft was a complete success. The Chinese Space Station welcomed its first foreign astronaut. What other highlights are there in this launch mission?
# 2 | 4,640,000 heat | 175 answers | Young people are beginning to persuade their parents to sell their vacant houses in the county and exchange them for cash to collect interest. Is it necessary to keep a house that has been vacant for many years?
# 3 | 2,400,000 heat | 192 answers | The AI short drama "Ugly Fox" sparked a lot of discussion after it became popular. What is the core reason why it can hit the audience?
# 4 | 2,290,000 heat | 28 answers | Trump publicly mentioned a Chinese dish with a beautiful "hat" and said it was delicious but couldn't name it. Which dish could it be?
# 5 | 2,180,000 heat | 175 answers | Why do I think Mr. Bean's performance is very vulgar and childish and not funny at all? Is it the difference between Chinese and Western cultures?

寻找开放式讨论

要找出那些虽受欢迎但回答较少的问题,你可以修改代码来计算每个回答的热度。这有助于识别仍处于开放讨论阶段的话题。

步骤 7:识别热门但回答较少的话题

将以下代码添加到你的脚本中:

for q in questions:
    q["heatPerAnswer"] = q["heat"] / max(q.get("answers") or 1, 1)

open_topics = sorted(questions, key=lambda q: q["heatPerAnswer"], reverse=True)[:2]
print("Hot but under-answered:")
for q in open_topics:
    print(f'{q["heatPerAnswer"]:10,.0f} heat/answer | {q.get("titleEn") or q["title"]}')

回答较少话题的示例输出

运行此部分时,你可能会看到:

Hot but under-answered:
  83,980 heat/answer | The launch of the Shenzhou 24 manned spacecraft was a complete success. The Chinese Space Station welcomed its first foreign astronaut. What other highlights are there in this launch mission?
  81,786 heat/answer | Trump publicly mentioned a Chinese dish with a beautiful "hat" and said it was delicious but couldn't name it. Which dish could it be?

随时间持续追踪

热榜在一天之中会不断变化。你可以在 Apify 中设置爬虫每小时运行一次。这样,你就可以追踪哪些话题随着时间的推移而上升或下降。

步骤 8:定时运行你的爬虫

  1. 在 Apify Console 中,导航至 Schedules 标签页。
  2. 为你的 actor 创建一个新的 schedule,使其每小时运行一次。这将帮助你定期捕获数据。
  3. 每次运行结果都会被保存,以便你分析随时间推移的趋势。

结语

通过遵循本指南,你可以有效地监控和分析知乎上的热门讨论。这些数据能为深入了解中国的公众舆论和热门话题提供宝贵的洞察。无论你是寻找趋势的营销人员,还是希望了解公众舆论的记者,知乎热榜都是一份极具价值的资源。

优势

  • 实时获取对中国热门讨论的洞察。
  • 了解公众对各类话题的舆论倾向。
  • 发现具有高互动潜力的开放式讨论。
  • 通过简单的 Python 脚本轻松获取数据。

局限性

  • 需要具备 Python 和网络爬虫的基础知识。
  • 仅能通过机器翻译获取英文数据,翻译结果可能并不总是准确。
  • 仅限于脚本中指定的排名靠前条目;获取更多条目需要调整代码。

注意事项

本文仅供教育参考。请确保将所有占位符替换为你的实际数据。在采信任何结论之前,务必对照原始来源进行核实。

常见问题解答

  • 什么是知乎? — 知乎是中国最大的问答平台,类似于 Quora。
  • 如何从知乎抓取数据? — 你可以使用 Python 和 Apify 平台来抓取知乎热榜。
  • 我可以从知乎热榜中提取哪些数据? — 你可以提取排名、标题、热度得分、回答数量等信息。
  • 数据是否有英文版本? — 是的,可以使用爬虫将数据翻译为英文。
  • 我需要知乎账号才能访问热榜吗? — 不需要,你无需拥有知乎账号即可访问热榜数据。
  • 热榜多长时间更新一次? — 热榜每隔几分钟就会更新一次,反映实时讨论情况。
  • 我可以随时间推移追踪趋势吗? — 可以,通过设置爬虫定期运行,你可以追踪话题随时间的变化情况。

标签

#python #webscraping #datascience

Free field guide

Prompt-Injection Defense Checklist

The controls that actually reduce the blast radius when your app feeds untrusted text to an LLM. Enter your email — you'll get the PDF instantly, plus new posts on AI, security & Linux.