1.4.3 • Published 6 years ago

zhihu v1.4.3

Weekly downloads
101
License
MIT
Repository
github
Last release
6 years ago

知乎数据 API 接口 (node.js)

知乎已经更新为 https, 本项目 \< 1.0.0 不能再使用了. 请升级

npm.io

npm.io

根据这些接口获取到知乎的数据,包括以下接口:

欢迎贡献代码,一起完善知乎的接口

Getting Started

DEMO

let zhihu = require('zhihu');

let username = 'shanejs';

zhihu.User.info(username).then(function(user){
  console.log(user);
});

/* You'll get
{
  answer:     14,
  post:       0,
  follower:   529,
  profileUrl: 'https://www.zhihu.com/people/shanejs',
  name:       '狂飙蜗牛',
  sex:        'male'
}
*/

Usage

User API

User.info(username)

根据用户名获取到用户的简要信息,username 为用户的唯一标识,参见个人主页的 url,或者设置中的个性域名(只能修改一次)。

比如请求这个用户 shanelau (zhihu.com/people/shanelau):

let zhihu = require('zhihu');
let name = 'shanelau';

zhihu.User.info(name).then(function(user){
  console.log(user);
});

请求成功将会返回:

/*
 * answer     (number) 答题数量
 * post       (number) 文章数量
 * follower   (number) 跟随者数量
 * profileUrl (string) 个人主页
 * name       (string) 名字
 * sex        (string) 性别
 */

 {
   answer:       5,
   post:         0,
   follower:     456,
   profileUrl:   'https://www.zhihu.com/people/shanelau1021',
   name:         '狂飙蜗牛',
   sex:          'male'
 }

User.questions

用户的提问列表 @TODO

User.answers

用户的回答列表 @TODO

User.zhuanlansFocus

用户关注的专栏 @TODO

User.topic

用户关注的话题信息 @TODO

Post API

Post.info(postUrl)

获取专栏文章的详细信息

  • postUrl 文章的url地址

Example

zhihu.Post.info(postUrl).then(function(data){
  // do something
});

Result

  • Object

example

Post.page(name, config)

获取专栏文章列表

  • name 专栏的英文名字, 例如:'bigertech'
  • config 可选,{object} ,默认值如下
{
  limit: 10   // 记录数
  offset: 0  // 偏移量
}

Example

demo

Post.likersDetail(postUrl, config)

获取专栏文章的点赞者的详细信息

  • postUrl 专栏文章的url地址
  • config 可选,配置对象,默认 {limit:10, offset:10}

Result

用户数组。结构与User.info接口返回的一致。

  • {Array} //User

Post.zhuanlanInfo(name)

获取专栏的信息

  • name 专栏的名字,比如 bigertech

Result

{
  followersCount: 22614,
  description: '',
  creator:
	 { bio: '魅族营销中心招募设计师',
	 hash: '29c3654588fd4246bb90cbd345242d65',
	 description: '',
	 profileUrl: 'http://www.zhihu.com/people/linan',
	 avatar:
	 { id: '24f3a654b',
	   template: 'http://pic2.zhimg.com/{id}\_{size}.jpg' },
	   slug: 'linan',
	   name: '李楠' },
	 topics: [],
	 href: '/api/columns/bigertech',
	 acceptSubmission: true,
	 slug: 'bigertech',
	 name: '笔戈科技',
	 url: '/bigertech',
	 avatar:
	 { id: 'a4bf61d95',
	   template: 'http://pic3.zhimg.com/{id}\_{size}.jpg' },
	   commentPermission: 'anyone',
	   following: false,
	   postsCount: 173,
	   canPost: false,
	   activateAuthorRequested: false }

Post.comments(postUrl, config)

获取专栏文章的评论信息

  • postUrl 专栏文章的url地址
  • config 可选,配置对象,默认 {limit:10, offset:10}
zhihu.Post.comments(`https://zhuanlan.zhihu.com/p/24241616?refer=chenyuz`).then(function(comments){
  console.log(comments);
});

Result
评论数组

[ 
  { liked: false,
    inReplyToCommentId: 0,
    featured: false,
    href: '/api/posts/24241616/comments/199226760',
    reviewing: false,
    disliked: false,
    dislikesCount: 0,
    id: 199226760,
    author:
     { profileUrl: 'https://www.zhihu.com/people/xu-xing-62-43',
       bio: '',
       hash: '6954117908c91a1c2897e466fc0545af',
       uid: 647461616195604500,
       isOrg: false,
       description: '',
       isOrgWhiteList: false,
       slug: 'xu-xing-62-43',
       avatar: [Object],
       name: 'SP fan' },
    content: 'ins即视感',
    createdTime: '2016-12-07T21:56:25+08:00',
    collapsed: false,
    likesCount: 1 
   },
    ...
    ...
]

Answer API

Answer.voters(answerId)

answerId 获取这个回答的点赞者。注意 answerIdurl_token 的区别,answerId 可以在 DOM Tree 中找到,具体的对应关系仍在探索中。知乎的一篇回答的 URL 结构一般是:

zhihu.com/question/12345/answer/67890
                   ^^^^^        ^^^^^
          question token        answer token

zhihu.com/answer/12306/voters_profile
                 ^^^^^
                 answer id

@TODO 实现知乎支持的更多参数,比如 offset 等

Question API

Question.answers(token, offset)

Question.answers(options)

获取该问题的回答列表

let Question = require('zhihu').Question;

Question.answers('19557271');
Question.answers('19557271', 10); // start from 10
Question.answers({token: '19557271', offset: 10});

Collection API

问题的收藏列表

url : http://www.zhihu.com/collection/25547043?page=1

Collection.getAllPageData

获取所有的页面数据,遍历所有的页面

Collection.getAllPageData(url);

Collection.getDataByPage

获取某一页的页面数据

let url = 'http://www.zhihu.com/collection/25547043?page=1';
Collection.getDataByPage(url);

Collection.getPagination

获取改收藏列表的分页信息

{
  pages: 总页数,
  current: 当前页面
}

Topic API

Topic.getTopicByID(topicID, page)

根据话题id获取该话题下的问题,话题id为唯一标识,参见话题的url

  • topicID 话题的ID

Example

请求这个话题:生活、艺术、文化与活动 topicID19778317

let topicID = '19778317';
zhihu.Topic.getTopicByID(topicID).then(function(result){
  console.log(result);
});

Result

参数说明

/* You'll get
 * name:      (string) 话题名称
 * page:      (number) 当前页数
 * totalPage: (number) 该话题下问题总页数
 * questions: (object) 当页问题
 * - title:   (string) 问题名字
 * - url:     (string) 问题链接
 * - postTime:(string) 问题最近更新时间
 */

{
  name: '生活、艺术、文化与活动',
  page: 1,
  totalPage: 47242,
  questions:
    { '0':
      { title: '为什么很多人能接受有过长期恋爱经历,却不能接受有过婚姻的人?',
        url: 'http://www.zhihu.com/question/27816723',
        postTime: '41 秒前' },
      '19':
      { title: '360卫士在C盘为什么不可以删掉?',
        url: 'http://www.zhihu.com/question/27816632',
        postTime: '5 分钟前' }
    }
}

贡献者

  1. shanelau
  2. Ivan Jiang (iplus26)
  3. sunchenguang

更新记录

2016.5.23

  1. 修复 https 问题
  2. 修改部分bug
  3. 加入 jscs 格式化代码风格

2015.10.15

  1. 新增收藏列表的数据抓取
  2. 查询某个收藏下的所有数据和分页数据

LICENSE

MIT

1.4.3

6 years ago

1.4.1

7 years ago

1.4.0

7 years ago

1.1.3

8 years ago

1.1.2

8 years ago

1.1.0

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago

0.0.21

9 years ago

0.0.20

9 years ago

0.0.19

9 years ago

0.0.18

9 years ago

0.0.17

9 years ago

0.0.16

9 years ago

0.0.15

9 years ago

0.0.14

9 years ago

0.0.13

9 years ago

0.0.12

9 years ago

0.0.11

9 years ago

0.0.10

9 years ago

0.0.9

9 years ago

0.0.8

9 years ago

0.0.7

9 years ago

0.0.6

9 years ago

0.0.5

9 years ago

0.0.4

9 years ago

0.0.3

9 years ago

0.0.2

9 years ago

0.0.0

10 years ago