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¶
Set Cookie¶
Set cookie for global request.
The cookie value must include z_c0 (authorization token) and _xsrf.
Parameters: val – Cookie string.
Raw Response¶
User¶
-
zhihuapi.
user
¶ alias of
User
-
class
zhihuapi.user.
User
(url_token)[source]¶ -
-
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.
-
Topic¶
-
zhihuapi.
topic
¶ alias of
Topic
-
class
zhihuapi.topic.
Topic
(topic_id)[source]¶ -
-
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.
-
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.
-
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.