Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
include README.md
include requirements.txt
include requirements.txt
include zhihu/models/*.js
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
requests>=2.13.0
beautifulsoup4>=4.5.3
lxml>=3.8.0
pyexecjs>=1.5.1
17 changes: 3 additions & 14 deletions zhihu/models/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -45,20 +44,16 @@ 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",
"timestamp": time_stamp,
"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),
Expand All @@ -69,12 +64,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')
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

经验证,这个js file只有在这里调用了

with open(path, "r") as f:
js = execjs.compile(f.read())
print("加密前")
print(data) # data是字典
# print()
text = "client_id=c3cef7c66a1843f8b3a9e6a1e3160e20&grant_type=password&timestamp={0}&" \
"source=com.zhihu.web&signature={1}&username={2}&password={3}&" \
"captcha={4}&lang=en&ref_source=homepage&utm_source=".format(
Expand All @@ -84,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,
Expand Down
12 changes: 1 addition & 11 deletions zhihu/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down