知乎 API v4 整理

作者:姚泽源

问题的回答

  • URL:https://www.zhihu.com/api/v4/questions/{qid}/answers
  • 参数:
    • limit:数量,最大 20
    • offset:起始位置,从零开始
    • sort_by{default, created},表示默认排序或者时间排序
    • include:额外信息,包括is_normal,is_sticky,collapsed_by,suggest_edit,comment_count,collapsed_counts,reviewing_comments_count,can_comment,content,editable_content,voteup_count,reshipment_settings,comment_permission,mark_infos,created_time,updated_time,relationship.is_author,voting,is_thanked,is_nothelp,upvoted_followees;author.is_blocking,is_blocked,is_followed,voteup_count,message_thread_token,badge[?(type=best_answerer)].topics

单个回答

  • URL:https://www.zhihu.com/api/v4/answers/{aid}
  • 参数:
    • include:额外信息,包括is_normal,is_sticky,collapsed_by,suggest_edit,comment_count,collapsed_counts,reviewing_comments_count,can_comment,content,editable_content,voteup_count,reshipment_settings,comment_permission,mark_infos,created_time,updated_time,relationship.is_author,voting,is_thanked,is_nothelp,upvoted_followees;author.is_blocking,is_blocked,is_followed,voteup_count,message_thread_token,badge[?(type=best_answerer)].topics

指定作者的回答

  • URL:https://www.zhihu.com/api/v4/members/{un}/answers
  • 参数:
    • limit:数量,最大 20
    • offset:起始位置,从零开始
    • sort_by{default, created},表示默认排序或者时间排序
    • include:额外信息,包括is_normal,suggest_edit,comment_count,collapsed_counts,reviewing_comments_count,can_comment,content,voteup_count,reshipment_settings,comment_permission,mark_infos,created_time,updated_time,relationship.voting,is_author,is_thanked,is_nothelp,upvoted_followees;author.badge[?(type=best_answerer)].topics

用户信息

  • URL:https://www.zhihu.com/api/v4/members/{un}
  • 参数:
    • include:额外信息,包括locations,employments,gender,educations,business,voteup_count,thanked_Count,follower_count,following_count,cover_url,following_topic_count,following_question_count,following_favlists_count,following_columns_count,avatar_hue,answer_count,articles_count,pins_count,question_count,columns_count,commercial_question_count,favorite_count,favorited_count,logs_count,included_answers_count,included_articles_count,included_text,message_thread_token,account_status,is_active,is_bind_phone,is_force_renamed,is_bind_sina,is_privacy_protected,sina_weibo_url,sina_weibo_name,show_sina_weibo,is_blocking,is_blocked,is_following,is_followed,is_org_createpin_white_user,mutual_followees_count,vote_to_count,vote_from_count,thank_to_count,thank_from_count,thanked_count,description,hosted_live_count,participated_live_count,allow_message,industry_category,org_name,org_homepage,badge[?(type=best_answerer)].topics

自身信息

  • URL:https://www.zhihu.com/api/v4/me
  • 参数:
    • include:额外信息,包括locations,employments,gender,educations,business,voteup_count,thanked_Count,follower_count,following_count,cover_url,following_topic_count,following_question_count,following_favlists_count,following_columns_count,avatar_hue,answer_count,articles_count,pins_count,question_count,columns_count,commercial_question_count,favorite_count,favorited_count,logs_count,included_answers_count,included_articles_count,included_text,message_thread_token,account_status,is_active,is_bind_phone,is_force_renamed,is_bind_sina,is_privacy_protected,sina_weibo_url,sina_weibo_name,show_sina_weibo,is_blocking,is_blocked,is_following,is_followed,is_org_createpin_white_user,mutual_followees_count,vote_to_count,vote_from_count,thank_to_count,thank_from_count,thanked_count,description,hosted_live_count,participated_live_count,allow_message,industry_category,org_name,org_homepage,badge[?(type=best_answerer)].topics

关注者信息

  • URL:https://www.zhihu.com/api/v4/members/{un}/followees
  • 参数:
    • limit:数量,最大 20
    • offset:起始位置,从零开始
    • include:额外信息,包括answer_count,articles_count,gender,follower_count,is_followed,is_following,badge[?(type=best_answerer)].topics

粉丝信息

  • URL:https://www.zhihu.com/api/v4/members/{un}/followers
  • 参数:
    • limit:数量,最大 20
    • offset:起始位置,从零开始
    • include:额外信息,包括answer_count,articles_count,gender,follower_count,is_followed,is_following,badge[?(type=best_answerer)].topics

收藏夹信息

  • URL:https://api.zhihu.com/collections/{cid}

收藏夹中的回答摘要

  • URL:https://api.zhihu.com/collections/{cid}/answers
  • 参数:
    • limit:数量,最大 20
    • offset:起始位置,从零开始

知乎话题

  • URL:https://www.zhihu.com/api/v4/topics/{tid}

知乎话题精华

  • URL:https://www.zhihu.com/api/v4/topics/{tid}/feeds/essence
  • 参数:
    • limit:数量,最大 20
    • offset:起始位置,从零开始
    • include:额外信息,包括data[?(target.type=topic_sticky_module)].target.data[?(target.type=answer)].target.content,relationship.is_authorized,is_author,voting,is_thanked,is_nothelp;data[?(target.type=topic_sticky_module)].target.data[?(target.type=answer)].target.is_normal,comment_count,voteup_count,content,relevant_info,excerpt.author.badge[?(type=best_answerer)].topics;data[?(target.type=topic_sticky_module)].target.data[?(target.type=article)].target.content,voteup_count,comment_count,voting,author.badge[?(type=best_answerer)].topics;data[?(target.type=topic_sticky_module)].target.data[?(target.type=people)].target.answer_count,articles_count,gender,follower_count,is_followed,is_following,badge[?(type=best_answerer)].topics;data[?(target.type=answer)].target.annotation_detail,content,hermes_label,is_labeled,relationship.is_authorized,is_author,voting,is_thanked,is_nothelp;data[?(target.type=answer)].target.author.badge[?(type=best_answerer)].topics;data[?(target.type=article)].target.annotation_detail,content,hermes_label,is_labeled,author.badge[?(type=best_answerer)].topics;data[?(target.type=question)].target.annotation_detail,comment_count
### 使用 Python 实现乎评论爬虫 要通过 Python 编写一个能够抓取乎评论的爬虫,可以从以下几个方面入手: #### 1. **分析目标网站** 在编写爬虫之前,需要先了解乎网页结构以及其 API 接口的工作方式。乎通常会使用动态加载的方式显示数据,因此直接解析 HTML 页面可能无法获取到完整的评论列表[^1]。 可以通过浏览器开发者工具查看网络请求,找到用于加载评论的数据接口 URL 和参数设置。这些接口一般返回 JSON 数据格式,便于后续处理。 #### 2. **准备必要的库** 为了完成此任务,需要用到一些常见的 Python 库来发送 HTTP 请求并解析响应内容: - `requests` 或者更高级别的异步框架如 `aiohttp` 来发起网络请求; - `json` 模块用来解码服务器返回的结果; - 可选地还可以引入 `pandas` 对采集后的数据做进一步整理存储等工作。 以下是所需安装的基础依赖项命令示例: ```bash pip install requests pandas ``` #### 3. **构建基本爬虫逻辑** 下面给出一段基础版代码片段作为参考起点,它展示了如何向指定的问题 ID 下面的所有评论发起 GET 请求,并提取其中的关键字段保存至本地 CSV 文件当中。 ```python import os import time from typing import List, Dict import requests import json def fetch_comments(question_id: str, offset=0) -> List[Dict]: url = f"https://www.zhihu.com/api/v4/questions/{question_id}/comments" headers = { 'User-Agent': ('Mozilla/5.0 (Windows NT 10.0; Win64; x64)' 'AppleWebKit/537.36 (KHTML, like Gecko)' 'Chrome/92.0.4515.131 Safari/537.36'), 'authorization': '' # 如果必要的话填入你的 token 这里 } params = {'order': 'created', 'limit': 20, 'offset': offset} response = requests.get(url=url, headers=headers, params=params) if not response.ok: raise Exception(f"Failed to load comments {response.status_code}") data = response.json() total_count = int(data['paging']['totals']) comment_list = [] for item in data["data"]: author_name = item['author']['member']['name'] content_text = item['content
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值