Skip to content

Commit b2964d3

Browse files
author
Luo
committed
修正auth.py 中的会重新登录问题
1 parent 55a7159 commit b2964d3

File tree

2 files changed

+28
-29
lines changed

2 files changed

+28
-29
lines changed

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)