Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions zhihu/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from .models import answer
from .models import zhihu
from .models.article import Article

Answer = answer.Answer
Zhihu = zhihu.Zhihu
28 changes: 28 additions & 0 deletions zhihu/models/article.py
Original file line number Diff line number Diff line change
@@ -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)
1 change: 0 additions & 1 deletion zhihu/models/zhihu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
8 changes: 8 additions & 0 deletions zhihu/test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from zhihu import Answer
from zhihu import Article
import unittest
import time

Expand Down Expand Up @@ -29,3 +30,10 @@ def test_vote_neutral_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())
5 changes: 5 additions & 0 deletions zhihu/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

class URL(object):
host = "https://www.zhihu.com"
zhuanlan_host = "https://zhuanlan.zhihu.com"

# 登录
@staticmethod
Expand Down Expand Up @@ -43,6 +44,10 @@ def follow(user_slug):
def vote_up(answer_id):
return URL.host + "/api/v4/answers/{id}/voters".format(id=answer_id)

@staticmethod
def article(post_id):
return URL.zhuanlan_host + '/api/posts/{id}'.format(id=post_id)

# 某答案下感谢答主
@staticmethod
def thank(answer_id):
Expand Down