diff --git a/zhihu/do_login.py b/zhihu/do_login.py deleted file mode 100755 index 77d36a4..0000000 --- a/zhihu/do_login.py +++ /dev/null @@ -1,47 +0,0 @@ -# -*- coding:utf-8 -*- -# python-version: 3.51 - -import requests -from bs4 import BeautifulSoup - -# 知乎登陆的时候并没有在前台做过多的加密,只是每次捕捉了它的xsrf就行了,和csdn的模拟登陆一样 -session = requests.session() - -# 要提交的参数 -headers = { - "User-Agent": ("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/602.4.8 (KHTML, like Gecko) " - "Version/10.0.3 Safari/602.4.8"), - "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", - "Host": 'www.zhihu.com', - 'Accept-Language': 'zh-cn', - "Accept-Encoding": 'gzip, deflate', - 'Cache-Control': 'max-age=0', - 'Upgrade-Insecure-Requests': '1' -} - -# 从原始界面拿到 _xsrf,第一次post -get_url = 'https://www.zhihu.com/#signin' - -phone_num = input('请输入你的用户名') -password = input('请输入你的密码') -remember_me = 'true' - -xsrf_url = session.get(get_url, headers=headers).text - -soup_one = BeautifulSoup(xsrf_url, 'html.parser') -xsrf = soup_one.body.find('div', {'class': 'index-main'}).find('div', {'class': 'index-main-body'}).form.find('input', { - 'name': '_xsrf'}).attrs['value'] - -post_data1 = { - 'phone_num': phone_num, - 'password': password, - 'remember_me': remember_me, - '_xsrf': xsrf -} - -r = session.post('https://www.zhihu.com/login/phone_num', data=post_data1, headers=headers) - -if r.status_code == 200: - print('模拟登陆成功') -else: - print('模拟登陆失败') diff --git a/zhihu/zhihu.py b/zhihu/zhihu.py new file mode 100755 index 0000000..6c6025b --- /dev/null +++ b/zhihu/zhihu.py @@ -0,0 +1,77 @@ +import requests , time +import hmac ,json +from bs4 import BeautifulSoup +from hashlib import sha1 + """ + info: + author:CriseLYJ + github:https://github.com/CriseLYJ/ + update_time:2019-3-6 + """ + +def get_captcha(data,need_cap): + ''' 处理验证码 ''' + if need_cap is False: + return + with open('captcha.gif','wb') as fb: + fb.write(data) + return input('captcha:') + +def get_signature(grantType,clientId,source,timestamp): + ''' 处理签名 ''' + + hm = hmac.new(b'd1b964811afb40118a12068ff74a12f4',None,sha1) + hm.update(str.encode(grantType)) + hm.update(str.encode(clientId)) + hm.update(str.encode(source)) + hm.update(str.encode(timestamp)) + + return str(hm.hexdigest()) + + + +def login(username,password,oncaptcha,sessiona,headers): + ''' 处理登录 ''' + + resp1 = sessiona.get('https://www.zhihu.com/signin',headers=headers) # 拿cookie:_xsrf + resp2 = sessiona.get('https://www.zhihu.com/api/v3/oauth/captcha?lang=cn',headers=headers) # 拿cookie:capsion_ticket + need_cap = json.loads(resp2.text)["show_captcha"] # {"show_captcha":false} 表示不用验证码 + + grantType = 'password' + clientId = 'c3cef7c66a1843f8b3a9e6a1e3160e20' + source ='com.zhihu.web' + timestamp = str((time.time()*1000)).split('.')[0] # 签名只按这个时间戳变化 + + captcha_content = sessiona.get('https://www.zhihu.com/captcha.gif?r=%d&type=login'%(time.time()*1000),headers=headers).content + + data = { + "client_id":clientId, + "grant_type":grantType, + "timestamp":timestamp, + "source":source, + "signature": get_signature(grantType,clientId,source,timestamp), # 获取签名 + "username":username, + "password":password, + "lang":"cn", + "captcha":oncaptcha(captcha_content,need_cap), # 获取图片验证码 + "ref_source":"other_", + "utm_source":"" + } + + print("**2**: "+str(data)) + print("-"*50) + resp = sessiona.post('https://www.zhihu.com/api/v3/oauth/sign_in',data,headers=headers).content + print(BeautifulSoup(resp,'html.parser')) + + print("-"*50) + return resp + +if __name__ == "__main__": + sessiona = requests.Session() + headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0','authorization':'oauth c3cef7c66a1843f8b3a9e6a1e3160e20'} + # 用户名密码换自己的就好了 + phone_num = input('请输入你的用户名') + password = input('请输入你的密码') + login(phone_num, password,get_captcha,sessiona,headers) + resp = sessiona.get('https://www.zhihu.com/inbox',headers=headers) # 登录进去了,可以看私信了 + print(BeautifulSoup(resp.content ,'html.parser'))