1- import requests ,time
1+ # -*- coding:UTF-8 -*-
2+
3+ import requests , time
4+ import hmac ,json
25from bs4 import BeautifulSoup
3- url = 'https://www.zhihu.com/login/email'
4- def get_captcha (data ):
6+ from hashlib import sha1
7+
8+
9+ def get_captcha (data ,need_cap ):
10+ ''' 处理验证码 '''
11+ if need_cap is False :
12+ return
513 with open ('captcha.gif' ,'wb' ) as fb :
614 fb .write (data )
7- return input ('captcha' )
15+ return input ('captcha:' )
16+
17+ def get_signature (grantType ,clientId ,source ,timestamp ):
18+ ''' 处理签名 '''
19+
20+ hm = hmac .new (b'd1b964811afb40118a12068ff74a12f4' ,None ,sha1 )
21+ hm .update (str .encode (grantType ))
22+ hm .update (str .encode (clientId ))
23+ hm .update (str .encode (source ))
24+ hm .update (str .encode (timestamp ))
825
9- def login (username ,password ,oncaptcha ):
10- sessiona = requests .Session ()
11- headers = {'User-Agent' :'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0' }
12- xyz = sessiona .get ('https://www.zhihu.com/#signin' ,headers = headers ).content
13- _xsrf = BeautifulSoup (sessiona .get ('https://www.zhihu.com/#signin' ,headers = headers ).content ,'html.parser' ).find ('input' ,attrs = {'name' :'_xsrf' }).get ('value' )
26+ return str (hm .hexdigest ())
27+
28+
29+
30+ def login (username ,password ,oncaptcha ,sessiona ,headers ):
31+ ''' 处理登录 '''
32+
33+ resp1 = sessiona .get ('https://www.zhihu.com/signin' ,headers = headers ) # 拿cookie:_xsrf
34+ resp2 = sessiona .get ('https://www.zhihu.com/api/v3/oauth/captcha?lang=cn' ,headers = headers ) # 拿cookie:capsion_ticket
35+ need_cap = json .loads (resp2 .text )["show_captcha" ] # {"show_captcha":false} 表示不用验证码
36+
37+ grantType = 'password'
38+ clientId = 'c3cef7c66a1843f8b3a9e6a1e3160e20'
39+ source = 'com.zhihu.web'
40+ timestamp = str ((time .time ()* 1000 )).split ('.' )[0 ] # 签名只按这个时间戳变化
41+
1442 captcha_content = sessiona .get ('https://www.zhihu.com/captcha.gif?r=%d&type=login' % (time .time ()* 1000 ),headers = headers ).content
43+
1544 data = {
16- "_xsrf" :_xsrf ,
17- "email" :username ,
45+ "client_id" :clientId ,
46+ "grant_type" :grantType ,
47+ "timestamp" :timestamp ,
48+ "source" :source ,
49+ "signature" : get_signature (grantType ,clientId ,source ,timestamp ), # 获取签名
50+ "username" :username ,
1851 "password" :password ,
19- "remember_me" :True ,
20- "captcha" :oncaptcha (captcha_content )
52+ "lang" :"cn" ,
53+ "captcha" :oncaptcha (captcha_content ,need_cap ), # 获取图片验证码
54+ "ref_source" :"other_" ,
55+ "utm_source" :""
2156 }
22- resp = sessiona .post ('https://www.zhihu.com/login/email' ,data ,headers = headers ).content
23- print (resp )
57+
58+ print ("**2**: " + str (data ))
59+ print ("-" * 50 )
60+ resp = sessiona .post ('https://www.zhihu.com/api/v3/oauth/sign_in' ,data ,headers = headers ).content
61+ print (BeautifulSoup (resp ,'html.parser' ))
62+
63+ print ("-" * 50 )
2464 return resp
2565
2666if __name__ == "__main__" :
27- login ('email' ,'password' ,get_captcha )
67+ sessiona = requests .Session ()
68+ headers = {'User-Agent' :'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0' ,'authorization' :'oauth c3cef7c66a1843f8b3a9e6a1e3160e20' }
69+
70+ login ('12345678@qq.com' ,'12345678' ,get_captcha ,sessiona ,headers ) # 用户名密码换自己的就好了
71+ resp = sessiona .get ('https://www.zhihu.com/inbox' ,headers = headers ) # 登录进去了,可以看私信了
72+ print (BeautifulSoup (resp .content ,'html.parser' ))
73+
74+
75+
76+
77+ ### chcp 65001 (win下改变cmd字符集)
78+ ### python c:\python34\login_zhihu.py
79+ ### 有非常无语的事情发生,还以为代码没生效
80+
0 commit comments