Giter Site home page Giter Site logo

zhihu-api's Introduction

zhihu-api

travis license

关于

Zhihu-API 的初衷是希望提供一套简洁、优雅的、Pythonic的API接口,面向的用户是对知乎数据有兴趣的群体,它可以用在数据分析、数据挖掘、增长黑客、以及希望通过程序自动完成知乎某个操作等应用场景。

注意:只支持Python3

安装

pip install -U zhihu
# 或者安装最新包
pip install git+git://github.com/lzjun567/zhihu-api --upgrade

快速上手

from zhihu import User
zhihu = User()

#获取用户基本信息
profile = zhihu.profile(user_slug="xiaoxiaodouzi")
print(profile)

>>>
{
    'name': '我是x',
     'headline': '程序员',
     'gender': -1,
     'user_type': 'people',
     'is_advertiser': False,
     'url_token': 'xiaoxiaodouzi',
     'id': '1da75b85900e00adb072e91c56fd9149',
     'is_org': False
}

# 发送私信
>>> zhihu.send_message(content="私信测试", user_slug="xiaoxiaodouzi")
<Response [200]>

# 关注用户
>>> zhihu.follow(user_slug="xiaoxiaodouzi")
{'follower_count': 12, 'followed': True}

# 取消关注
>>> zhihu.unfollow(user_slug="xiaoxiaodouzi")
{'follower_count': 11, 'followed': False}


>>> from zhihu import Answer
>>> answer = Answer(url="https://www.zhihu.com/question/62569341/answer/205327777")

# 赞同回答
>>> answer.vote_up()
{'voting': 1, 'voteup_count': 260}

# 反对
>>> answer.vote_down()
{'voting': -1, 'voteup_count': 259}

# 中立
>>> answer.vote_neutral()
{'voting': 0, 'voteup_count': 260}

# 感谢回答
>>> answer.thank()
{'is_thanked': True}

# 取消感谢
>>> answer.thank_cancel()
{'is_thanked': False}

# 提取回答中的图片
>>> answer.images(path="images")
['8160c14ea69b3a6674152f2c1ae6cd7a_b.jpg']


>>> from zhihu import User
>>> zhihu = User()
# 关注用户
>>>zhihu.follow(user_slug="zhijun-liu")

# 取消关注
>>>zhihu.unfollow(user_slug="zhijun-liu")

# 粉丝列表
>>> zhihu.followers(user_slug="zhijun-liu")

贡献者

欢迎 PR, 所有贡献者都将出现在这里,排名不分先后

交流

群已经加不进,可以先加微信:lzjun567 拉你入群

群

zhihu-api's People

Contributors

bigborg avatar chenghengchao avatar cw1997 avatar excitedfrog avatar jeffery9 avatar kellyhwong avatar lzjun567 avatar maxpoon avatar oopswc avatar sherrywong1220 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

zhihu-api's Issues

麻烦看看api是不是被知乎gank了

我执行import的时候
from zhihu import Zhihu
全部提示importerror:
ImportError: cannot import name 'Zhihu'
麻烦看看是我的安装库出问题了还是啥?
环境是fedora25,python3.5.3

关于README.md中的错别字

在 贡献者 一栏中,原文为“欢迎 PR, 所有贡献者都将出现在这里,排名部分先后”,应为“排名不分先后”吧

尝试发送私信时又上重新登录,且尝试多次仍不能成功。

尝试发送私信时又上重新登录,且尝试多次仍不能成功。

执行 zhihu.user(user_slug='xiaoxiaodouzi')之后 并未出现以下用户信息项,请问我是否登录成功?
目前此项目是不是还能正常使用,还是知乎网站做了限制 ?

{'avatar_url_template': 'https://pic1.zhimg.com/v2-ca13758626bd7367febde704c66249ec_{size}.jpg',
'badge': [],
'name': '我是小号',
'headline': '程序员',
……
}

以下是报错及输入输出全文 :

In [1]: from zhihu import Zhihu

In [2]: zhihu = Zhihu()

In [3]: zhihu.user(user_slug='xiaoxiaodouzi')
请输入email或者手机号码:ah×××××@qq.com
请输入密码:×××××××
验证码:e6gr

In [4]: zhihu.send_message('hello',user_slug='xiaoxiaodouzi')
请输入email或者手机号码:ah×××××@qq.com
请输入密码:×××××××
验证码:×××××××
请输入email或者手机号码:ah×××××@qq.com
请输入密码:×××××××
验证码:8xp6

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-f5af582b8420> in <module>()
----> 1 zhihu.send_message('hello',user_slug='xiaoxiaodouzi')

/usr/local/lib/python3.5/dist-packages/zhihu/auth.py in wrapper(self, *args, **kwargs)
     27                 success = account.Account().login(email, password)
     28         if success:
---> 29             result = func(self, *args, **kwargs)
     30             return result
     31 

/usr/local/lib/python3.5/dist-packages/zhihu/models/common.py in send_message(self, content, user_id, profile_url, user_slug, **kwargs)
     30         if user_id is None:
     31             user_slug = self._user_slug(profile_url) if user_slug is None else user_slug
---> 32             user_id = self._user_id(user_slug)
     33 
     34         data = {"type": "common", "content": content, "receiver_hash": user_id}

/usr/local/lib/python3.5/dist-packages/zhihu/models/__init__.py in _user_id(self, user_slug)
     86         """
     87         profile = self.user(user_slug=user_slug)
---> 88         user_id = profile.get("id")
     89         return user_id
     90 

AttributeError: 'NoneType' object has no attribute 'get'

导入时出错

from zhihu import Account
Traceback (most recent call last):
File "", line 1, in
File "C:\Python27\lib\site-packages\zhihu_init_.py", line 1, in
from zhihu.models import answer
File "C:\Python27\lib\site-packages\zhihu\models_init_.py", line 22, in
from zhihu.error import ZhihuError
File "C:\Python27\lib\site-packages\zhihu\models\zhihu.py", line 6, in
from ..auth import need_login
File "C:\Python27\lib\site-packages\zhihu\auth.py", line 6, in
from zhihu.models.account import Account
File "C:\Python27\lib\site-packages\zhihu\models\account.py", line 6, in
from ..models import Model
ImportError: cannot import name Model

README.md文件疑似有误

README.md文件疑似有误

关注专栏与取消关注专栏在此文件中是同一个命令,如下所示。
关注专栏

column.follow()
关注专栏成功

取消关注专栏

column.follow()
取消关注专栏成功

正在学习此项目,从开始,发现问题再提。
感谢作者的辛勤工作!

【新功能】代理池

貌似现在没有支持代理池的功能?如果 没有的话,那我就先着手做这个了。

调用response = self._session.get(url),获取的网页内容,和浏览器获取的不一样。

我想定义一个类,用来实现获取指定用户的“提问”列表。
比如,获取用户heikehuawuya所有提问的标题。
url = https://www.zhihu.com/people/heikehuawuya/asks

我的代码都是依托zhihu-api框架,自己的代码就是asks_list()函数。
调用response = self._session.get(url)时,获取的网页内容,和浏览器获取的不一样。
调试很久,找不出原因,请大家帮忙!
代码如下。

class Userasks(Model):
def init(self, slug=None, url=None):
slug = slug if slug is not None else self._extract_slug(url)
if not slug:
raise ZhihuError("没有指定用户的的slug或者url")
self.slug = slug
super(Userasks, self).init()

@staticmethod
def _extract_id(url):
    """
    从url中提取目标id
    :param url: 
    :return: 
    """
    pattern = re.compile("https://www.zhihu.com/people/(\w+).*?/")
    match = pattern.search(url)
    return match.group(1) if match else None

def asks_list(self, **kwargs):
    question_list = []
    url = URL.user_asks(self.slug)
    response = self._session.get(url)
    soup = BeautifulSoup(response.content, "html.parser")
    tag_list = soup.find_all("div", "ContentItem")
    for name in tag_list.find_all("a"):
        question_list.append(name.get_text())

    return question_list

`

关于收藏的获取

包括:

  1. 我关注的收藏夹
  2. 我创建的收藏夹
  3. 收藏夹中的文章地址

其中收藏夹包含的数据:

  1. 创建者
  2. 内容数量
  3. 关注人数
  4. 文章信息

没有overide 为什么要 super(Account, self)._get_xsrf()

没有overide 为什么要 super(Account, self)._get_xsrf()

data = {
"fullname": name,
"phone_num": phone_num,
"password": password,
"_xsrf": super(Account, self)._get_xsrf(),
"captcha": super(Account, self)._get_captcha(_type="register"),
"captcha_source": "register",
}

调用发送消息(send_message)API时出错

我在调用发送消息(send_message)API时,出现了错误。我认真看了看报错,没找出根源。

但我发现某处代码好像有问题:
`在Model类中,_user_id方法中,有一行代码:
“profile = self.user(user_slug=user_slug)"

   类中并没有user这一方法呢。

登录时报错

输入完密码,报错

60 response = self.get(url or URL.index())
61 soup = BeautifulSoup(response.content, "lxml")
---> 62 xsrf = soup.find('input', attrs={"name": "_xsrf"}).get("value")
63 return xsrf
64

AttributeError: 'NoneType' object has no attribute 'get'

我打印过html, 确实没有'input', attrs={"name": "_xsrf"}这样的标签。
如果用try语句跳过,可以显示验证码,但是输入验证码之后还是报错。

关于单元测试

单元测试里面实现的那些类,测试入口在哪里,不需要写类的实例吗?
一点疑问:测试文件夹下的py文件名和modules下的一模一样,这个有什么说法吗?
@lzjun567

sublime 运行报错

from zhihu import Zhihu
sublime 运行报错
ImportError: cannot import name 'Zhihu'

在命令行里面运行这是没有问题的。

跑简单的用例都出错,不知道其他人有没遇到

其他情况zhihu.profile()调用后,输入账号密码也是报差不多的错误

$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.

from zhihu import Account
account = Account()
account.login("yxxxxxxxxxx","pxxxxxx")
Traceback (most recent call last):
File "", line 1, in
File "/home/yiwu/.local/lib/python3.5/site-packages/zhihu/models/account.py", line 29, in login
return self._login_api(account, password)
File "/home/yiwu/.local/lib/python3.5/site-packages/zhihu/models/account.py", line 35, in _login_api
_xsrf, _dc0 = self._get_xsrf_dc0()
File "/home/yiwu/.local/lib/python3.5/site-packages/zhihu/models/base.py", line 84, in _get_xsrf_dc0
xsrf = response.cookies["_xsrf"]
File "/home/yiwu/.local/lib/python3.5/site-packages/requests/cookies.py", line 329, in getitem
return self._find_no_duplicates(name)
File "/home/yiwu/.local/lib/python3.5/site-packages/requests/cookies.py", line 400, in _find_no_duplicates
raise KeyError('name=%r, domain=%r, path=%r' % (name, domain, path))
KeyError: "name='_xsrf', domain=None, path=None"

NoneType' object has no attribute 'get'

File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\zhihu\models\account.py", line 31, in login
return self._login_phone(account, password)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\zhihu\models\account.py", line 37, in _login_phone
'_xsrf': self._get_xsrf(),
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\zhihu\models\base.py", line 62, in _get_xsrf
xsrf = soup.find('input', attrs={"name": "_xsrf"}).get("value")
AttributeError: 'NoneType' object has no attribute 'get'

实现上传知乎头像功能报错

  1. 代码逻辑:手机通过fiddler代理访问知乎,在手机知乎app上传头像,通过fiddler抓包。
    在代码里使用抓包得到的cookie实现上传头像功能

  2. 代码:
    详见附件
    其中avatar.txt是实现代码(py文件不让上传,麻烦下载后后缀名改为py),avatar.jpg是要上传的头像,fiddler.txt是fiddler抓包数据

  3. 报错
    avatar_status = 400
    avatar_text = {"error":{"message":"参数错误","code":4000}}

avatar.txt
avatar
fiddler.txt

规范

接口API的信息可以写的更加规范一点

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.