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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ pip install git+git://github.com/lzjun567/zhihu-api --upgrade
>>> zhihu.send_message("你好,问候2", user_slug="xiaoxiaodouzi")
```

**关注用户和被关注用户**

```
>>> zhihu.follows("高日日")
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

follow vs follows,小伙子改个名吧。。。

{"following": 30,
"followers": 4}
```

**关注用户**
```
>>> zhihu.follow(user_slug="xiaoxiaodouzi")
Expand Down
22 changes: 22 additions & 0 deletions zhihu/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,25 @@ def _execute(self, method="post", url=None, data=None, data_type=RequestDataType
else:
r = getattr(self._session, method)(url, data=data, **kwargs)
return r
def _get_token(self, Name=None, Headers=settings.HEADERS):
assert type(Name) == str, "name的类型必须为字符串形式"
"""
根据用户名,获取最相关的用户的token。
:return: token
>>> _get_token(Name="高日日")
>>> 返回/people/gao-ri-ri-78
"""
url = 'https://www.zhihu.com/search?type=people&q={}'.format(Name)
data = requests.get(url, headers=Headers)
soup = BeautifulSoup(data.text, 'lxml')
yonghu = soup.select('a[class="name-link author-link"]')
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yonghu 是什么鬼。。。user 是哪里不好?

pattern = '<a.*?href="([^"]*)".*?>(?:[\S\s]*?)</a>'
token = None
for i in yonghu:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

两个点。

  1. 既然下面用 for 来迭代了,这里显然应该是用复数:usersuser_list 之类的东西。
  2. for 循环中,用 i 通常都是 index 的缩写,这里显然是对应的 user 对象。所以,user 就好。for user in users: pass

i = str(i)
mat = re.findall(pattern, i)
token = mat
if token[0]:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的逻辑是,找到第一个 match(对,mat 也没几个人知道是啥,不要随便缩写。。。)然后直接 return match[0]。所以,其实 token 这个变量完全没有存在的意义。下面这样写就够了。

for user in users:
    match = re.findall(pattern, str(user))
    if match[0]:
        return match[0]
return None

break

return token[0]
34 changes: 34 additions & 0 deletions zhihu/models/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,37 @@ def unfollow(self, user_slug=None, profile_url=None, **kwargs):
return response.json()
else:
raise ZhihuError("操作失败:%s" % response.text)

def follows(self, user_name=None):
"""
用户所关注人数,被关注的人数
:param user_slug:
:param profile_url:
:return: {"follower_count": int}

>>> follows(user_name = "高日日")
"""
if not user_name:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

既然要求一定要填 user_name ,为什么要填默认值。。。

raise ZhihuError("至少指定一个关键字参数")

user_slug = self._get_token(user_name)

response = requests.get(URL.user_followed_number(user_slug), headers={'User-Agent':
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

headers 的值应该有固定的?如果没有的话,找个地方放一下,然后在这里(以及其他地方)引用

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

恩,因为作者之前的失效了,我自己测试随便写了一个。

'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36'
})
if response.ok:
soup = BeautifulSoup(response.text, 'lxml')
init_data_renshu = soup.select('div[class="NumberBoard-value"]')

dicts = dict()
counts = 0
for i in init_data_renshu:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里直接写两遍 i.get_next() 就可以了吧。。。没必要用 for 循环,看着很别扭。还强行设置了一个 counts...
顺便,小伙子你大概不知道 enumerate 这个东西?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry,比较菜

if counts < 1:
dicts["following"] = str(i.get_text())
else:
dicts["followers"] = str(i.get_text())
counts += 1

print(dicts)
else:
raise ZhihuError("操作失败:%s" % response.text)
7 changes: 6 additions & 1 deletion zhihu/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ def profile(user_slug):
@staticmethod
def follow_people(user_slug):
return URL.host + "/api/v4/members/{user_slug}/followers".format(user_slug=user_slug)


# 用户关注页面
@staticmethod
def user_followed_number(slug):
return URL.host + "{slug}/following".format(slug=slug)

# 赞同/反对/中立
@staticmethod
def vote_up(answer_id):
Expand Down