V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
MarkTonyFromMars
V2EX  ›  程序员

知乎专栏 API 的简单分析+示例项目

  •  
  •   MarkTonyFromMars · 2016-07-11 15:05:52 +08:00 · 4881 次点击
    这是一个创建于 3093 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我的示例项目地址:知乎专栏 GitHub

    我的 GitHub 主页: https://github.com/marktony ,大爷,进来看看嘛

    声明

    声明:以下所有 API 均由 知乎(Zhihu.INC) 提供,本人通过非正常手段获取。获取与共享的行为有侵犯知乎权益的嫌疑。若被告知停止使用与共享,本人将及时删除整个项目。请您知悉相关情况,遵守知乎的协议。 API 仅供学习交流使用,请勿用作商业用途。

    说明

    • 知乎专栏的 API 均以 json 格式输出
    • http method 均为 GET

    分析

    1.获取专栏信息

    https://zhuanlan.zhihu.com/api/columns/专栏名
    

    如:

    https://zhuanlan.zhihu.com/api/columns/wooyun
    

    注意使用 https,在旧版的 API 中使用的是 http 。

    得到的信息:

    {
        "followersCount": 31506,
        "description": "写点有用的东西。\nhttp://wooyun.org",
        "creator":
        {
            "bio": "请砍我的头。",
            "hash": "37841ec3e75e6583e627f64a21343b4d",
            "description": "",
            "profileUrl": "http://www.zhihu.com/people/____",
            "avatar":
            {
                "id": "6c83b4b34",
                "template": "http://pic1.zhimg.com/{id}_{size}.jpg"
            },
            "slug": "____",
            "name": "长短短"
        },
        "topics":
        [
            {
                "url": "http://www.zhihu.com/topic/19670074",
                "id": "19670074",
                "name": "乌云 (WooYun)"
            },
            {
                "url": "http://www.zhihu.com/topic/19554927",
                "id": "19554927",
                "name": "网络安全"
            }
        ],
        "href": "/api/columns/wooyun",
        "acceptSubmission": true,
        "slug": "wooyun",
        "name": "乌云君",
        "url": "/wooyun",
        "avatar":
        {
            "id": "0ec77eef1",
            "template": "https://pic2.zhimg.com/{id}_{size}.jpg"
        },
        "commentPermission": "anyone",
        "following": true,
        "postsCount": 50,
        "canPost": false,
        "activateAuthorRequested": false
    }
    
    followersCount :关注者数量
    description:简介
    creator :专栏所有者
    avatar :头像
    topics :话题
    

    在获取头像地址时,将 id 拼接到 template 中即可获取到 url ,例如:

    https://pic2.zhimg.com/424c70919_l.jpg
    https://pic2.zhimg.com/424c70919_m.jpg
    https://pic2.zhimg.com/424c70919_s.jpg
    

    分别对应 large,middle,small 。 也可以不传入 size 参数,默认获取最大(large)。

    2.获取某个专栏的文章列表

    http://zhuanlan.zhihu.com/api/columns/专栏名 /posts?limit=数量&offset=从哪里开始
    
    如从 wooyun 获取前 10 条:
    
    http://zhuanlan.zhihu.com/api/columns/wooyun/posts?limit=10&offset=0
    
    

    limit 为获取数量限制,测试超过 10 也是可以的, offset 偏移量,从哪里开始获取。

    得到的信息:

    [
        {
            "rating": "like",
            "sourceUrl": "",
            "publishedTime": "2015-12-30T19:54:14+08:00",
            "links":
            {
                "comments": "http://zhuanlan.zhihu.com/api/columns/wooyun/posts/20460225/comments"
            },
            "author":
            {
                "bio": "信息安全,鸡尾酒,电吉他,摄影。",
                "hash": "1e373c5c6d9af3c8beaeb9aadc0890ac",
                "description": "写文字对于我来说挺痛苦的。",
                "profileUrl": "http://www.zhihu.com/people/fenggou",
                "avatar":
                {
                    "id": "939c20127",
                    "template": "http://pic4.zhimg.com/{id}_{size}.jpg"
                },
                "slug": "fenggou",
                "name": "fenggou"
            },
            "column":
            {
              "slug": "wooyun",
              "name": "乌云君"
            },
            "topics": [],
            "title": "用 iPhone 的,丢过或者要丢的赶紧进来瞅瞅(二)",
            "titleImage": "https://pic1.zhimg.com/aa0da1510a8d64c96bf811072a35addc_b.jpg",
            "summary": "简介省略",
            "content": "内容省略",
            "url": "/wooyun/20460225",
            "state": "published",
            "href": "/api/columns/wooyun/posts/20460225",
            "meta":
            {
              "previous": null,
              "next": null
            },
            "commentPermission": "anyone",
            "snapshotUrl": "",
            "canComment": true,
            "slug": 20460225,
            "commentsCount": 18,
            "likesCount": 152
        }
        ...
    ]
    

    得到了一个 json 数组,需要注意的是 titleImage 的值可能为"".

    3.获取特定文章

    将已经得到的 slug 值进行拼接,即可获取特定文章信息。

    https://zhuanlan.zhihu.com/api/posts/SLUG
    

    如:

    https://zhuanlan.zhihu.com/api/posts/20460225
    

    得到的信息:

    {
    	"isTitleImageFullScreen": false,
    	"rating": "none",
    	"titleImage": "https://pic1.zhimg.com/aa0da1510a8d64c96bf811072a35addc_r.jpg",
    	"links":
      {
        "comments": "/api/posts/20460225/comments"
      },
      "reviewers": [],
      "topics": [],
      "titleImageSize":
      {
        "width": 719,
        "height": 393
      },
      "href": "/api/posts/20460225",
      "author":
      {
        "bio": "bio",
        "hash": "1e373c5c6d9af3c8beaeb9aadc0890ac",
        "description": "description",
        "profileUrl": "https://www.zhihu.com/people/fenggou",
        "avatar":
        {
          "id": "939c20127",
          "template": "https://pic4.zhimg.com/{id}_{size}.jpg"
        },
        "slug": "fenggou",
        "name": "fenggou"
      },
      "content": "content",
      "state": "published",
      "sourceUrl": "",
      "canComment": true,
      "snapshotUrl": "",
      "slug": 20460225,
      "publishedTime": "2015-12-30T19:54:14+08:00",
      "url": "/p/20460225",
      "title": "title",
      "lastestLikers":
      [
      	{
      		"bio": "\u6c6a\u54c1\u4ea7",
      		"hash": "dfdcff7bf3fe6e6b47c3c85bd686cd82",
      		"description": "",
      		"profileUrl": "https://www.zhihu.com/people/li-lin-30-84",
      		"avatar":
      		{
      			"id": "f408bb13c37e131b3b461b5882a6f9d1",
      			"template": "https://pic2.zhimg.com/{id}_{size}.jpg"
      		},
      		"slug": "li-lin-30-84",
      		"name": "\u6797\u674e"
      	},
      	{
      		"bio": "\u6253\u6742",
      		"hash": "930502c3bb2cd807e2dc0ecf192f9867",
      		"description": "description",
      		"profileUrl": "https://www.zhihu.com/people/hu-xiao-yu-74-49",
      		"avatar":
      		{
      			"id": "87fd0f2a73313d5889e9a571bde43a8a",
      			"template": "https://pic3.zhimg.com/{id}_{size}.jpg"
      		},
      		"slug": "hu-xiao-yu-74-49",
      		"name": "\u80e1\u6653\u5b87"
      	},
      	...
      ],
      "summary": "summary",
      "column":
      {
      	"slug": "wooyun",
      	"name": "\u4e4c\u4e91\u541b"
      },
      "meta":
      {
      	"previous":
      	{
      		"isTitleImageFullScreen": false,
      		"rating": "none",
      		"titleImage": "https://pic3.zhimg.com/a30ea3ed0b85fb52baaae17f24691a12_r.jpg",
      		"links":
      		{
      			"comments": "/api/posts/20416511/comments"
      		},
      		"topics": [],
      		"href": "/api/posts/20416511",
      		"author":
      		{
      			"bio": "bio",
      			"hash": "1e373c5c6d9af3c8beaeb9aadc0890ac",
      			"description": "description",
      		  "profileUrl": "https://www.zhihu.com/people/fenggou",
      		  "avatar":
        		{
        			"id": "939c20127",
        			"template": "https://pic4.zhimg.com/{id}_{size}.jpg"
        		},
        		"slug": "fenggou",
        		"name": "fenggou"
      		},
      		"content": "content",
      		"state": "published",
      		"sourceUrl": "",
      		"canComment": true,
      		"snapshotUrl": "",
      		"slug": 20416511,
      		"publishedTime": "2015-12-14T19:13:38+08:00",
      		"url": "/p/20416511",
      		"title": "title",
      		"summary": "summary",
          "column":
          {
          	"slug": "wooyun",
          	"name": "\u4e4c\u4e91\u541b"
          },
          "meta":
          {
            "previous": null, "next": null
          },
          "commentPermission": "anyone",
          "commentsCount": 0,
          "likesCount": 0
      },
      "next":
      {
        "isTitleImageFullScreen": false,
        "rating": "none",
        "titleImage": "https://pic4.zhimg.com/04129f190219918c6f2c597daed1a6ab_r.jpg",
        "links":
        {
          "comments": "/api/posts/20581814/comments"
        },
        "topics": [],
        "href": "/api/posts/20581814",
        "author":
        {
          "bio": "bio",
          "hash": "1e373c5c6d9af3c8beaeb9aadc0890ac",
          "description": "description",
          "profileUrl": "https://www.zhihu.com/people/fenggou",
          "avatar":
          {
            "id": "939c20127",
            "template": "https://pic4.zhimg.com/{id}_{size}.jpg"
          },
          "slug": "fenggou",
          "name": "fenggou"
        },
        "content": "content",
        "state": "published",
        "sourceUrl": "",
        "canComment": true,
        "snapshotUrl": "",
        "slug": 20581814,
        "publishedTime": "2016-02-18T15:18:42+08:00",
        "url": "/p/20581814",
        "title": "title",
        "summary": "summary",
        "column":
        {
    	  "slug": "wooyun",
    	  "name": "\u4e4c\u4e91\u541b"
        },
        "meta":
        {
          "previous": null, "next": null
        },
        "commentPermission": "anyone",
        "commentsCount": 0,
        "likesCount": 0
        }
      },
      "commentPermission": "anyone",
      "commentsCount": 26,
      "likesCount": 240
    }
    
    

    通过示例数据可以看出:获取到的不仅仅是一篇文章,同时还获取到了上一篇和下一篇的值。

    4.获取评论信息 获取到文章信息后,有一个字段 comments,它所对应的值即为获取评论的地址

    	"comments": "/api/posts/20460225/comments"
    

    示例:

    	https://zhuanlan.zhihu.com/api/posts/20460225/comments
    

    获取到的 json 数组:

    [
    	{
    		"liked": false, 
    		"author": 
    			{
    				"bio": "Working in the dark to serve the light.", 
    				"hash": "ab0f1a39723020f3ed9c1ba1f0948072", 
    				"description": "", 
    				"profileUrl": "https://www.zhihu.com/people/lan-diao-ye-qu",
    				"avatar": 
    					{
    						"id": "dfb1995bdb6da62008d9a99741a3b96f", 
    						"template": "https://pic4.zhimg.com/{id}_{size}.jpg"
    					},
    				"slug": "lan-diao-ye-qu",
    				"name": "\u963f\u5361\u59c6\u9a91\u58eb"
    			}, 
    			"content": "content", 
    			"href": "/api/posts/20460225/comments/111076966", 
    			"createdTime": "2015-12-30T20:01:32+08:00", 
    			"id": 111076966, 
    			"likesCount": 0
    	},
    	...
    ]
    

    通过这种方式获取,返回的 array 的 size 只有 20.想要获取其他的评论方法和获取文章的方法一样,拼接 url 。

    示例:

    https://zhuanlan.zhihu.com/api/posts/20460225/comments?limit=30&offset=0
    

    limit 为要获取的数量, offset 为偏移量。

    7 条回复    2016-07-11 22:07:15 +08:00
    Lonely
        1
    Lonely  
       2016-07-11 15:14:19 +08:00 via Android
    你还要发几遍?
    MarkTonyFromMars
        2
    MarkTonyFromMars  
    OP
       2016-07-11 15:17:35 +08:00
    @Lonely 不好意思 如果有打扰到你的话 说声抱歉 我也是想要贡献一份自己的力量嘛 自己平时找 API 的时候找的好辛苦,发出来让更多的人看到,可以少走一些路
    liteneo
        3
    liteneo  
       2016-07-11 15:34:47 +08:00
    看到标题就知道是你了,换着花样发了多少遍了
    MarkTonyFromMars
        4
    MarkTonyFromMars  
    OP
       2016-07-11 15:49:34 +08:00
    @liteneo 不好意思 如果有打扰到你的话 说声抱歉 我也是想要贡献一份自己的力量嘛 自己平时找 API 的时候找的好辛苦,发出来让更多的人看到,可以少走一些路 抱歉
    aitaii
        5
    aitaii  
       2016-07-11 16:08:30 +08:00
    只要发一次就可以搜到了,明显的推广贴。
    sunyue
        6
    sunyue  
       2016-07-11 21:54:47 +08:00
    这个 API 是知乎提供的啊。抓包可以抓包的吗?还是内部途径知道的
    MarkTonyFromMars
        7
    MarkTonyFromMars  
    OP
       2016-07-11 22:07:15 +08:00 via Android
    @sunyue 可以抓包的 charles
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2762 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 11:57 · PVG 19:57 · LAX 03:57 · JFK 06:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.