From 62502b532969161afa585f775682a318c2c894f6 Mon Sep 17 00:00:00 2001 From: BigBorg <770188954@qq.com> Date: Sat, 22 Apr 2017 16:20:04 +0800 Subject: [PATCH 1/2] Get Post --- README.md | 11 +++++++++++ zhihu/models/zhihu.py | 14 +++++++++++++- zhihu/url.py | 5 +++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d96968e..155cc01 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,18 @@ vote_down ### 中立 vote_neutral +### 获取文章 +``` +from zhihu.url import URL +from zhihu.models.zhihu import Zhihu + +zhihuClient = Zhihu() +jsondata = zhihuClient.post(26459091) +jsondata.keys() +>>> dict_keys(['excerptTitle', 'summary', 'tipjarState', 'reviewers', 'author', 'slug', 'links', 'meta', 'reviewingCommentsCount', 'url', 'titleImageSize', 'rating', 'title', 'likesCount', 'collapsedCount', 'canComment', 'content', 'topics', 'pageCommentsCount', 'commentsCount', 'sourceUrl', 'state', 'href', 'commentPermission', 'publishedTime', 'snapshotUrl', 'isTitleImageFullScreen', 'titleImage', 'lastestLikers']) + +``` ## TODO diff --git a/zhihu/models/zhihu.py b/zhihu/models/zhihu.py index aae30a7..ad8c4d5 100644 --- a/zhihu/models/zhihu.py +++ b/zhihu/models/zhihu.py @@ -5,7 +5,6 @@ from ..error import ZhihuError from ..url import URL - class Zhihu(Model): @need_login def send_message(self, content, user_id=None, profile_url=None, user_slug=None, **kwargs): @@ -81,3 +80,16 @@ def follow(self, user_slug=None, profile_url=None, **kwargs): return response.json() else: self.logger.error(u"关注失败, status code: %s" % response.status_code) + + @need_login + def post(self, post_id=None): + if not post_id: + raise ZhihuError("指定文章id") + + self._session.headers['Host'] = 'zhuanlan.zhihu.com' + response = self._session.get(URL.post(post_id)) + self._session.headers['Host'] = 'www.zhihu.com' + if response.ok: + return response.json() + else: + self.logger.error(u"文章获取失败, status code: %s" % response.status_code) \ No newline at end of file diff --git a/zhihu/url.py b/zhihu/url.py index f87b8e5..40cc337 100644 --- a/zhihu/url.py +++ b/zhihu/url.py @@ -7,6 +7,7 @@ class URL(object): host = "https://www.zhihu.com" + zhuanlan_host = "https://zhuanlan.zhihu.com" # 登录 @staticmethod @@ -42,3 +43,7 @@ def follow(user_slug): @staticmethod def vote_up(answer_id): return URL.host + "/api/v4/answers/{id}/voters".format(id=answer_id) + + @staticmethod + def post(post_id): + return URL.zhuanlan_host + '/api/posts/{id}'.format(id=post_id) From 63cccaa9cccb25d690d614fdf142b4f13101ade5 Mon Sep 17 00:00:00 2001 From: BigBorg <770188954@qq.com> Date: Mon, 24 Apr 2017 09:59:04 +0800 Subject: [PATCH 2/2] Article class --- zhihu/__init__.py | 1 + zhihu/models/article.py | 28 ++++++++++++++++++++++++++++ zhihu/models/zhihu.py | 13 ------------- zhihu/test.py | 8 ++++++++ zhihu/url.py | 2 +- 5 files changed, 38 insertions(+), 14 deletions(-) create mode 100644 zhihu/models/article.py diff --git a/zhihu/__init__.py b/zhihu/__init__.py index eb1035f..c256d2b 100644 --- a/zhihu/__init__.py +++ b/zhihu/__init__.py @@ -2,6 +2,7 @@ from .models import answer from .models import zhihu +from .models.article import Article Answer = answer.Answer Zhihu = zhihu.Zhihu diff --git a/zhihu/models/article.py b/zhihu/models/article.py new file mode 100644 index 0000000..b7c7925 --- /dev/null +++ b/zhihu/models/article.py @@ -0,0 +1,28 @@ +from zhihu.url import URL +from zhihu.auth import need_login +from . import Model +import re + +class Article(Model): + def __init__(self, id=None, url=None): + self.id = str(id) + if id is None and url: + pattern = re.compile("https://zhuanlan.zhihu.com/p/(\d+)") + match = pattern.search(url) + if match: + self.id = match.group(1) + super(Article, self).__init__() + + @need_login + def request(self, **kwargs): + url = URL.article(self.id) + self._session.headers['Host'] = 'zhuanlan.zhihu.com' + response = self._session.get(URL.article(self.id)) + self._session.headers['Host'] = 'www.zhihu.com' + if response.ok: + data = response.json() + for key,val in data.items(): + self.__dict__[key] = val # ['excerptTitle', 'summary', 'tipjarState', 'reviewers', 'author', 'slug', 'links', 'meta', 'reviewingCommentsCount', 'url', 'titleImageSize', 'rating', 'title', 'likesCount', 'collapsedCount', 'canComment', 'content', 'topics', 'pageCommentsCount', 'commentsCount', 'sourceUrl', 'state', 'href', 'commentPermission', 'publishedTime', 'snapshotUrl', 'isTitleImageFullScreen', 'titleImage', 'lastestLikers'] + return data + else: + self.logger.error(u"文章获取失败, status code: %s" % response.status_code) \ No newline at end of file diff --git a/zhihu/models/zhihu.py b/zhihu/models/zhihu.py index ad8c4d5..f5e5b6e 100644 --- a/zhihu/models/zhihu.py +++ b/zhihu/models/zhihu.py @@ -80,16 +80,3 @@ def follow(self, user_slug=None, profile_url=None, **kwargs): return response.json() else: self.logger.error(u"关注失败, status code: %s" % response.status_code) - - @need_login - def post(self, post_id=None): - if not post_id: - raise ZhihuError("指定文章id") - - self._session.headers['Host'] = 'zhuanlan.zhihu.com' - response = self._session.get(URL.post(post_id)) - self._session.headers['Host'] = 'www.zhihu.com' - if response.ok: - return response.json() - else: - self.logger.error(u"文章获取失败, status code: %s" % response.status_code) \ No newline at end of file diff --git a/zhihu/test.py b/zhihu/test.py index 8cd25c7..1baf864 100644 --- a/zhihu/test.py +++ b/zhihu/test.py @@ -1,4 +1,5 @@ from zhihu import Answer +from zhihu import Article import unittest import time @@ -29,3 +30,10 @@ def test_vote_nature_with_url(self): data = Answer(url="https://www.zhihu.com/question/19761434/answer/14005147").vote_neutral() self.assertIn("voting", data) self.assertIn("voteup_count", data) + +class ArticleTestCase(unittest.TestCase): + def test_request(self): + article = Article(id=26096748) + data = article.request() + self.assertIn("content", data.keys()) + self.assertIn("content", article.__dict__.keys()) \ No newline at end of file diff --git a/zhihu/url.py b/zhihu/url.py index 40cc337..fb71337 100644 --- a/zhihu/url.py +++ b/zhihu/url.py @@ -45,5 +45,5 @@ def vote_up(answer_id): return URL.host + "/api/v4/answers/{id}/voters".format(id=answer_id) @staticmethod - def post(post_id): + def article(post_id): return URL.zhuanlan_host + '/api/posts/{id}'.format(id=post_id)