Skip to content

Commit 33773a0

Browse files
committed
Merge pull request egrcc#16 from LuoZijun/patch-4
修正重新登录问题
2 parents 7a7430e + 13db981 commit 33773a0

3 files changed

Lines changed: 96 additions & 57 deletions

File tree

README.rst

Lines changed: 68 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
zhihu-python:获取知乎信息
22
===============================
33

4+
:Author: `egrcc <https://github.com/egrcc>`_ ( `微博 <http://weibo.com/u/2948739432>`_ | `电邮 <zhaolujun1994@gmail.com>`_ )
5+
:Committer: `Eureka22 <https://github.com/Eureka22>`_ , `lufo816 <https://github.com/lufo816>`_ , `LuoZijun <https://github.com/LuoZijun>`_
6+
:Update: 09/09 2015
7+
8+
9+
.. contents::
10+
11+
412
介绍
513
----
614

@@ -40,42 +48,74 @@ zhihu-python 采用 python2.7 编写,用来方便地获取知乎上各种内
4048
4149
导出的markdown,txt文件示例请见该项目的markdown,text文件夹。当然,想要知道某大V关注了那些人,提了什么问题也不在话下,详情请见:快速开始。
4250

43-
依赖
44-
-----
4551

46-
- 使用 `Beautiful Soup 4 <http://www.crummy.com/software/BeautifulSoup/>`_ 解析 html 文档
47-
- 使用 `requests <https://github.com/kennethreitz/requests>`_ 处理 http 请求
48-
- 使用 `html2text <https://github.com/aaronsw/html2text>`_ 进行格式转换
4952

50-
没有的话可以使用 pip 安装:
53+
快速开始
54+
---------
5155

52-
.. code-block:: bash
56+
**准备**
5357

54-
$ pip install requests
55-
$ pip install beautifulsoup4
56-
$ pip install html2text
5758

58-
或者使用
59+
**Tips** :
5960

60-
.. code-block:: bash
61+
1. 确保你的系统里面已经安装了 `Python2.7 <https://www.python.org/>`_ ,不同作业系统如何安装不再赘述。
62+
2. 检查你系统中 `python` 和 `pip` 的版本, 如果不属于 `python2.7` , 请在执行代码范例时,自行将 `python` 和 `pip` 分别替换成 `python2.7` 和 `pip2` 。
63+
3. 确保你的系统中安装了 `git` 程序 以及 `python-pip` 。
6164

62-
$ pip install -r requirements.txt
6365

64-
**注意** :如果同时安装了 python3 和 python2 , pip 命令可能默认安装的是 python3 版本的包,故需将上述命令中的 pip 换成pip2 (比如在我的Ubuntu上)。
66+
**克隆本项目**
6567

66-
快速开始
67-
---------
6868

69-
zhihu-python 主要文件为 zhihu.py ,配置文件为 config.ini , 将这两个文件下载到你的工作目录,并修改
70-
config.ini 文件中的 email 为你的知乎账户邮箱,修改 password 为你的知乎账户密码(用作模拟登录)。
69+
.. code:: bash
70+
71+
git clone git@github.com:egrcc/zhihu-python.git
72+
cd zhihu-python
73+
74+
75+
**解决依赖**
76+
77+
* `Beautiful Soup 4 <http://www.crummy.com/software/BeautifulSoup/>`_
78+
* `requests <https://github.com/kennethreitz/requests>`_
79+
* `html2text <https://github.com/aaronsw/html2text>`_
80+
* `termcolor <https://pypi.python.org/pypi/termcolor>`_
81+
82+
83+
.. code:: bash
84+
85+
sudo pip install -r requirements.txt
86+
87+
88+
Or
89+
90+
.. code:: bash
91+
92+
sudo pip2 install -r requirements.txt
93+
94+
95+
96+
**登录知乎**
97+
98+
99+
登录 `知乎` 生成身份信息, 保存在当前目录的 `cookies` 文件中。
100+
101+
.. code:: bash
102+
103+
python auth.py
104+
105+
106+
**执行测试**
107+
108+
109+
.. code:: bash
110+
111+
python test.py
112+
113+
不出意外,一切应该完美运行 :))
71114

72-
(2015.2.1更新)由 `@Eureka22 <https://github.com/Eureka22>`_ 添加了cookies支持,若由于验证码原因出现登录失败,请查看浏览器的cookies,填写 config.ini 文件中的 cookies 项后重试(当然,你的cookies可能与 cogfig.ini 文件中的某些项不同,比如没有“c_c”,可能会多出“_ga”,把你的补充上去即可)。如能正常登录,cookies 项可以不填写。
73115

74-
**注意** :一定记得修改config.ini文件,否则无法正常使用。
75116

76-
77117
Question:获取问题信息
78-
~~~~~~~~~~~~~~~~~~~~~~~~
118+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
79119

80120
Question 代表一个问题,处理知乎问题相关操作。创建一个 Question 对象需传入该问题的 url ,如:
81121

@@ -142,7 +182,7 @@ Question 代表一个问题,处理知乎问题相关操作。创建一个 Ques
142182
143183
144184
Answer:获取答案信息
145-
~~~~~~~~~~~~~~~~~~~~~
185+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
146186

147187
Answer 代表了一个答案,处理知乎答案相关操作。创建一个 Answer 对象需传入该答案的 url ,如:
148188

@@ -193,7 +233,7 @@ Answer 代表了一个答案,处理知乎答案相关操作。创建一个 Ans
193233
194234
195235
User:获取用户信息
196-
~~~~~~~~~~~~~~~~~~~~~~~
236+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
197237

198238
User 代表一个用户,处理用户相关操作。创建一个 User 对象需传入该用户的 url ,如:
199239

@@ -268,7 +308,7 @@ User 代表一个用户,处理用户相关操作。创建一个 User 对象需
268308
269309
270310
Collection:获取收藏夹信息
271-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
311+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
272312

273313
Collection 代表一个收藏夹,处理收藏夹相关操作。创建一个 Collection 对象需传入该收藏夹的 url ,如:
274314

@@ -312,7 +352,7 @@ Collection 代表一个收藏夹,处理收藏夹相关操作。创建一个 Co
312352
313353
314354
综合实例
315-
~~~~~~~~~~~~~~~
355+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
316356

317357
将 Question , Answer , User , Collection 结合起来使用。实例如下:
318358

@@ -625,6 +665,6 @@ zhihu.Collection ---- 知乎收藏夹操作类
625665
联系我
626666
----------
627667

628-
- 微博:`@egrcc <http://weibo.com/u/2948739432>`_
629-
- github:`@egrcc <https://github.com/egrcc>`_
668+
- 微博:http://weibo.com/u/2948739432
669+
- github:https://github.com/egrcc
630670
- email:zhaolujun1994@gmail.com

auth.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@
77

88
# requirements
99
import requests, termcolor
10-
# try:
11-
# from bs4 import BeautifulSoup
12-
# except:
13-
# import BeautifulSoup
14-
# Darwin platform
15-
# BeautifulSoup = BeautifulSoup.BeautifulSoup
10+
1611

1712
requests = requests.Session()
1813
requests.cookies = cookielib.LWPCookieJar('cookies')
@@ -80,9 +75,12 @@ def download_captcha():
8075
"""
8176
System platform: https://docs.python.org/2/library/platform.html
8277
"""
78+
Logging.info(u"正在调用外部程序渲染验证码 ... ")
8379
if platform.system() == "Linux":
84-
os.system("see %s &" % image_name )
80+
Logging.info(u"Command: xdg-open %s &" % image_name )
81+
os.system("xdg-open %s &" % image_name )
8582
elif platform.system() == "Darwin":
83+
Logging.info(u"Command: open %s &" % image_name )
8684
os.system("open %s &" % image_name )
8785
elif platform.system() == "SunOS":
8886
os.system("open %s &" % image_name )
@@ -180,7 +178,7 @@ def read_account_from_config_file(config_file="config.ini"):
180178
if os.path.exists(config_file) and os.path.isfile(config_file):
181179
Logging.info(u"正在加载配置文件 ...")
182180
cf.read(config_file)
183-
#cookies = cf._sections['cookies']
181+
184182
email = cf.get("info", "email")
185183
password = cf.get("info", "password")
186184
if email == "" or password == "":
@@ -197,6 +195,7 @@ def read_account_from_config_file(config_file="config.ini"):
197195
def login(account=None, password=None):
198196
if islogin() == True:
199197
Logging.success(u"你已经登录过咯")
198+
return True
200199

201200
if account == None:
202201
(account, password) = read_account_from_config_file()
@@ -228,4 +227,5 @@ def login(account=None, password=None):
228227
return True
229228

230229
if __name__ == "__main__":
231-
login()
230+
# login(account="xxxx@email.com", password="xxxxx")
231+
login()

zhihu.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,39 +59,38 @@
5959
from bs4 import BeautifulSoup
6060
except:
6161
import BeautifulSoup
62-
# Darwin platform
63-
#BeautifulSoup = BeautifulSoup.BeautifulSoup
6462

65-
reload(sys)
66-
sys.setdefaultencoding('utf8')
63+
# module
64+
from auth import islogin
65+
from auth import Logging
66+
67+
68+
"""
69+
Note:
70+
1. 身份验证由 `auth.py` 完成。
71+
2. 身份信息保存在当前目录的 `cookies` 文件中。
72+
3. `requests` 对象可以直接使用,身份信息已经自动加载。
6773
74+
By Luozijun (https://github.com/LuoZijun), 09/09 2015
6875
76+
"""
6977
requests = requests.Session()
7078
requests.cookies = cookielib.LWPCookieJar('cookies')
7179
try:
7280
requests.cookies.load(ignore_discard=True)
7381
except:
74-
print u"ERROR: 你还没有登录知乎哦 ..."
75-
print u"INFO: 执行 `python auth.py` 即可以完成登录。"
82+
Logging.error(u"你还没有登录知乎哦 ...")
83+
Logging.info(u"执行 `python auth.py` 即可以完成登录。")
7684
raise Exception("无权限(403)")
7785

78-
"""
79-
Note:
80-
全局变量 `session` 以及 `cookie` 还有 `create_session` 函数 可以逐步在代码移除,当前用作过渡。
81-
82-
User/Question ... 等 抓取类 可以直接 使用 `requests.get/requests.post`
83-
来抓取知乎链接。身份cookies已经自动设定。
84-
85-
身份Cookies文件保存于 `cookies`。
86-
"""
87-
session = requests
88-
8986

90-
def create_session():
91-
print "NOTE: 该函数已经不再需要。"
92-
return True
87+
if islogin() != True:
88+
Logging.error(u"你的身份信息已经失效,请重新生成身份信息( `python auth.py` )。")
89+
raise Exception("无权限(403)")
9390

9491

92+
reload(sys)
93+
sys.setdefaultencoding('utf8')
9594

9695
class Question:
9796
url = None

0 commit comments

Comments
 (0)