From b154615d1fa4b8c3b4ac9134a383a97cc7afebbb Mon Sep 17 00:00:00 2001 From: tianchen Date: Sun, 17 Nov 2019 09:54:13 -0800 Subject: [PATCH 1/4] add js file to manifest --- MANIFEST.in | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/MANIFEST.in b/MANIFEST.in index 3d387c3..fe481ea 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,2 +1,3 @@ include README.md -include requirements.txt \ No newline at end of file +include requirements.txt +include zhihu/models/*.js \ No newline at end of file From 1795e3311c7a0511a4b669e4badebd8c6957ac24 Mon Sep 17 00:00:00 2001 From: tianchen Date: Sun, 17 Nov 2019 10:08:26 -0800 Subject: [PATCH 2/4] delete dead code and clean print statement --- zhihu/models/account.py | 5 +---- zhihu/models/base.py | 12 +----------- 2 files changed, 2 insertions(+), 15 deletions(-) diff --git a/zhihu/models/account.py b/zhihu/models/account.py index 80eeb7d..f5774ad 100644 --- a/zhihu/models/account.py +++ b/zhihu/models/account.py @@ -69,12 +69,9 @@ def _login_api(self, account, password): def _login_execute(self, url=None, data=None): # 要进行加密 path = os.path.join(os.path.split( - os.path.realpath(__file__))[0], 'get_formdata.js') + os.path.realpath(__file__))[0], 'encrypt.js') with open(path, "r") as f: js = execjs.compile(f.read()) - print("加密前") - print(data) # data是字典 - # print() text = "client_id=c3cef7c66a1843f8b3a9e6a1e3160e20&grant_type=password×tamp={0}&" \ "source=com.zhihu.web&signature={1}&username={2}&password={3}&" \ "captcha={4}&lang=en&ref_source=homepage&utm_source=".format( diff --git a/zhihu/models/base.py b/zhihu/models/base.py index f9e243d..f171fa4 100644 --- a/zhihu/models/base.py +++ b/zhihu/models/base.py @@ -62,19 +62,9 @@ def _get_captcha(self, _type="login"): os.startfile('captcha.jpg') captcha = input("输入验证码:") data = {"input_text": captcha} - # data = json.dumps(data) - - # 要进行加密 - path = os.path.join(os.path.split( - os.path.realpath(__file__))[0], 'encrypt.js') - with open(path, "r") as f: - js = execjs.compile(f.read()) - print(data) - # data = js.call('Q', urlencode(data)) + response = self.post('https://www.zhihu.com/api/v3/oauth/captcha?lang=en', headers=self.headers, data=data) - print("看这里 输入验证码返回值") - print(response.json()) return captcha @staticmethod From dba8f68e92cd7d5e2c2eef266a8e21c0cde98f33 Mon Sep 17 00:00:00 2001 From: tianchen Date: Sun, 17 Nov 2019 10:11:45 -0800 Subject: [PATCH 3/4] add needed package --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index e109f99..10740ee 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ requests>=2.13.0 beautifulsoup4>=4.5.3 lxml>=3.8.0 +pyexecjs>=1.5.1 \ No newline at end of file From e17e610e3d3f28303618e3bcde21f3f052666571 Mon Sep 17 00:00:00 2001 From: tianchen Date: Sun, 17 Nov 2019 10:16:27 -0800 Subject: [PATCH 4/4] delete dead code and clean print statement --- zhihu/models/account.py | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/zhihu/models/account.py b/zhihu/models/account.py index f5774ad..edb0d4b 100644 --- a/zhihu/models/account.py +++ b/zhihu/models/account.py @@ -36,7 +36,6 @@ def login(self, account, password): def _login_api(self, account, password): time_stamp = str(int((time.time() * 1000))) - # _dc0 没有用 _xsrf, _ = self._get_xsrf_dc0( url="https://www.zhihu.com/signup?next=%2F") self.headers.update({ @@ -45,10 +44,8 @@ def _login_api(self, account, password): "X-Xsrftoken": _xsrf }) captcha = self._get_captcha() - # self.headers.update({"x-zse-83": "3_2.0"}) # 不带会有"请求参数异常,请升级客户端后重试" self.headers.update({'x-zse-83': '3_1.1', }) # 不带会有"请求参数异常,请升级客户端后重试" - print("获取好了captcha再更新headers,现在的headers") - print(self.headers) + data = { "client_id": "c3cef7c66a1843f8b3a9e6a1e3160e20", "grant_type": "password", @@ -56,9 +53,7 @@ def _login_api(self, account, password): "source": "com.zhihu.web", "password": password, "username": quote(account), - # "captcha": "", "lang": "en", - # "lang": "cn", "ref_source": "homepage", "utm_source": "", "signature": self._get_signature(time_stamp), @@ -81,10 +76,7 @@ def _login_execute(self, url=None, data=None): headers=self.headers, cookies=self.cookies) result = r.json() # TODO 改成logger - print(r) - print("看这里 登录返回值") - print(result) - print(r.content) + if r.status_code == 201: self.cookies.save(ignore_discard=True) # 保存登录信息cookies self.cookies.load(filename=settings.COOKIES_FILE,