zhihu API

This is just an UNOFFICIAL API for zhihu. View source on Github.

Node.js version is also available.

Installation

$ pip install zhihuapi

Quich Start

import zhihuapi as api

with open('cookie') as f:
    api.cookie(f.read())

data = api.user('zhihuadmin').profile()
print(data)

The result is:

{
    "url_token": "zhihuadmin",
    "avatar_url": "https://pic3.zhimg.com/34bf96bf5584ac4b5264bd7ed4fdbc5a_is.jpg",
    "avatar_url_template": "https://pic3.zhimg.com/34bf96bf5584ac4b5264bd7ed4fdbc5a_{size}.jpg",
    "type": "people",
    "name": "知乎小管家",
    "headline": "欢迎反馈问题和建议!",
    "is_org": false,
    "url": "https://www.zhihu.com/people/zhihuadmin",
    "badge": [
        {
            "type": "identity",
            "description": "知乎官方帐号"
        }
    ],
    "user_type": "people",
    "is_advertiser": false,
    "id": "3d198a56310c02c4a83efb9f4a4c027e"
}

Note that you must set cookie before sending any HTTP request. In order to set cookie correctly, you should:

  • Sign in with your zhihu account.
  • Open Chrome DevTools (or substitutes with other browsers) and navigate to Network panel.
  • Select a valid request (a request with host be www.zhihu.com), and copy its Cookie value in Request Headers.
  • DONOT use document.cookie since some cookie values are httponly.

API

Raw Response

zhihuapi.raw(val)[source]

Set _raw for global request.

Whether or not return raw response body. This is only available for zhihu v4 apis which return JSON format responses.

Parameters:val – A boolean value.

User

zhihuapi.user

alias of User

class zhihuapi.user.User(url_token)[source]
profile()[source]

Get profile of this user.

detail()[source]

Get detailed profile of this user.

activities(after_id=None, before_id=None)[source]

Get this user’s activities.

Parameters:
  • after_id – The upper bound of the activity id.
  • before_id – The lower bound of the activity id.
Returns:

A list of activities.

questions(offset=0)[source]

Get questions that this user asked.

Parameters:offset – An integer.
Returns:A list of questions.
answers(offset=0)[source]

Get answers that this user answered.

Parameters:offset – An integer.
Returns:A list of answers.
articles(offset=0)[source]

Get atricles that this user post.

Parameters:offset – An integer.
Returns:A list of articles.
collections(offset=0)[source]

Get collections owned by this user.

Parameters:offset – An integer.
Returns:A list of collections.
followers(offset=0)[source]

Get followers.

Parameters:offset – An integer.
Returns:A list of users.
followees(offset=0)[source]

Get followees.

Parameters:offset – An integer.
Returns:A list of users.
columns(offset=0)[source]

Get columns owned by this user and its contributions.

Parameters:offset – An integer.
Returns:A list of columns and contributions.
following_columns(offset=0)[source]

Get columns that this user followed.

Parameters:offset – An integer.
Returns:A list of columns.
following_topics(offset=0)[source]

Get topics that this user followed and its controbutions.

Parameters:offset – An integer.
Returns:A list of topics and contributions.
following_questions(offset=0)[source]

Get questions that this user followed.

Parameters:offset – An integer.
Returns:A list of questions.
following_collections(offset=0)[source]

Get collections that this user followed.

Parameters:offset – An integer.
Returns:A list of collections.

Topic

zhihuapi.topic

alias of Topic

class zhihuapi.topic.Topic(topic_id)[source]
classmethod root()[source]

The root topic.

hierarchy()[source]

Get the hierarchy of this topic.

followers(start='', offset=0)[source]

Get followers of this topic.

Parameters:
  • start – Timestamp in seconds.
  • offset – An integer.
Returns:

A list of users.

top_answers(page=1)[source]

Get top answers of this topic.

Parameters:page – The page number.
Returns:A list of answers.
hot_answers(offset='')[source]

Get hot answers of this topic.

Parameters:offset – Answer score in float number.
Returns:A list of answers.
new_answers(offset='')[source]

Get newest answers of this topic.

Parameters:offset – Answer score in float number.
Returns:A list of answers.
pending_question(page=1)[source]

Get pending questions of this topic.

For more information about what is a pending question, see: https://www.zhihu.com/question/40470324

Parameters:page – The page number.
Returns:A list of questions
hot_pending_question(page=1)[source]

Get hot pending questions of this topic.

For more information about what is a pending question, see: https://www.zhihu.com/question/40470324

Parameters:page – The page number.
Returns:A list of questions

Question

zhihuapi.question

alias of Question

class zhihuapi.question.Question(question_id)[source]
answers_byvote(offset=0)[source]

Get answers by voteup weights.

Parameters:offset – An integer.
Returns:A list of answers.
answers_bypage(page=1)[source]

Get answers by page (or created time).

Parameters:page – Page number.
Returns:A list of answers.
detail()[source]

Get detail information of this question.

Returns:Detailed question information.
followers(offset=0)[source]

Get users that followed this question.

Parameters:offset – An integer.
Returns:A list of users.

Answer

zhihuapi.answer

alias of Answer

class zhihuapi.answer.Answer(answer_id)[source]
voters(offset=0)[source]

Get voters of this answer.

Parameters:offset – An integer.
Returns:A list of voters.
comments(offset=0)[source]

Get comments of this answer.

Parameters:offset – An integer.
Returns:A list of comments.
static explore_day(offset=0)[source]

Explore hot answers in this day.

Parameters:offset – An integer.
Returns:A list of answers.
static explore_month(offset=0)[source]

Explore hot answers in this month.

Parameters:offset – An integer.
Returns:A list of answers.

Collection

zhihuapi.collection

alias of Collection

class zhihuapi.collection.Collection(collection_id)[source]
answers(page=1)[source]

Get answers in this collection.

Parameters:page – Page number.
Returns:A list of answers.

Column

zhihuapi.column

alias of Column

class zhihuapi.column.Column(slug)[source]
info()[source]

Get column information

pins()[source]

Pin top posts.

posts(offset=0)[source]

Posts in this column.

Parameters:offset – An integer.
Returns:A list of posts.
authors()[source]

Authors in this column.

Action

zhihuapi.action.follow(url_token)[source]

Follow a user.

Parameters:url_token – The url token (or slug) of the user.
zhihuapi.action.unfollow(url_token)[source]

Unfollow a user.

Parameters:url_token – The url token (or slug) of the user.
zhihuapi.action.message(user_id, content)[source]

Send message to a user.

Parameters:
  • user_id – Hash id of the user.
  • content – Message content in string.
zhihuapi.action.vote(answer_id, val)[source]

Vote an answer.

Parameters:
  • answer_id – The id of the answer.
  • val – Voting type in number. 1 for voting up, 0 for voting neutral, -1 for voting down.
zhihuapi.action.vote_up(answer_id)[source]

Vote up an answer.

Parameters:answer_id – The id of the answer.
zhihuapi.action.vote_neutral(answer_id)[source]

Vote neutral an answer.

Parameters:answer_id – The id of the answer.
zhihuapi.action.vote_down(answer_id)[source]

Vote down an answer.

Parameters:answer_id – The id of the answer.