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 diff --git a/requirements.txt b/requirements.txt index eb952bf..1612281 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,4 @@ beautifulsoup4>=4.5.3 lxml>=3.8.0 PyExecJS DecryptLogin + diff --git a/zhihu/models/account.py b/zhihu/models/account.py index 37d9508..65ba750 100644 --- a/zhihu/models/account.py +++ b/zhihu/models/account.py @@ -52,7 +52,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({ @@ -61,10 +60,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", @@ -72,9 +69,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), @@ -88,9 +83,6 @@ def _login_execute(self, url=None, data=None): 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( @@ -100,10 +92,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, 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