使用requests的session机制模拟登陆知乎
来源:互联网 发布:js原型面试题 编辑:程序博客网 时间:2024/05/16 18:19
开发环境
- Python版本: python3.6
- Python库: requests, pillow, re
- 浏览器: Google Chrome 62.0.3202.94
- 调试工具:pycharm 2017.1.3
- 电脑系统:Windows 10 64位系统
模拟登陆代码
import requeststry: import cookielibexcept: import http.cookiejar as cookielibimport resession = requests.session()session.cookies = cookielib.LWPCookieJar(filename="cookies.txt")try: session.cookies.load(ignore_discard=True)except: print ("cookie未能加载")headers = { 'Host': "www.zhihu.com", 'Referer': "https://www.zhihu.com/signin?next=/", 'user-agent': "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Mobile Safari/537.36"}def is_login(): #通过个人中心页面返回状态码来判断是否为登录状态 inbox_url = 'https://www.zhihu.com/inbox' response = session.get(inbox_url, headers=headers, allow_redirects=False) if response.status_code != 200: return False else: return Truedef get_xsrf(): #获取xsrf code response = session.get("https://www.zhihu.com/signin?next=/#signin", headers=headers) match_obj = re.match('.*name="_xsrf" value="(.*?)"', response.text) if match_obj: return (match_obj.group(1)) else: return ""def get_index(): response = session.get("https://www.zhihu.com", headers=headers) with open("index_page.html", "wb") as f: f.write(response.text.encode("utf-8")) print ("ok")def get_captcha(): import time t = str(int(time.time()*1000)) captcha_url = "https://www.zhihu.com/captcha.gif?r={0}&type=login".format(t) t = session.get(captcha_url, headers=headers) with open("captcha.jpg","wb") as f: f.write(t.content) f.close() from PIL import Image try: im = Image.open('captcha.jpg') im.show() im.close() except: pass captcha = input("输入验证码\n>") return captchadef zhihu_login(account, password): #知乎登录 if re.match("^1\d{10}",account): print ("手机号码登录") post_url = "https://www.zhihu.com/login/phone_num" post_data = { "_xsrf": get_xsrf(), "phone_num": account, "password": password, "captcha":get_captcha() } else: if "@" in account: #判断用户名是否为邮箱 print("邮箱方式登录") post_url = "https://www.zhihu.com/login/email" post_data = { "_xsrf": get_xsrf(), "email": account, "password": password } response_text = session.post(post_url, data=post_data, headers=headers) session.cookies.save()zhihu_login("187********", "******")# get_index()is_login()# get_captcha()
代码分析
访问知乎登陆界面
https://www.zhihu.com/#signin
按 F12 调试工具
点击: 登陆 – 使用密码登陆
在Network选项中, 勾选 preserve log (点击登陆会有页面会转跳,清空数据)
点击登陆之后,查看 XHR 参数, 我们会看到 phone_num 这个url传递的参数带有登陆信息
有四个参数: _xsrf 、password、 captcha_type、phone_num
第一个参数,当我们查看知乎登陆界面的源代码时, 搜索这个关键词就能查到。
验证码
知乎登陆有中文验证码和英文验证码请求,上述代码使用英文验证码请求的
阅读全文
0 0
- 使用requests的session机制模拟登陆知乎
- python模拟登陆知乎和CSDN【urllib,re,requests】
- 利用python requests库模拟登陆知乎
- requests模拟登陆(session+url_token)
- Python使用Requests第三方库自动登陆知乎
- 使用Python的Requests包模拟登陆
- 使用python完成知乎模拟登陆
- 模拟登陆知乎
- 使用requests和scrapy模拟知乎登录
- 【python爬虫01】使用requests库模拟登录知乎
- 使用requests模拟登陆github
- 使用 python requests 模拟登陆
- 使用requests模拟登陆csdn
- 【scrapy】模拟登陆知乎
- python---模拟登陆知乎
- python 模拟知乎登陆
- selenium模拟登陆知乎
- 使用OKHttp模拟登陆知乎,兼谈OKHttp中Cookie的使用!
- Java基础总结第(2)天
- HDU 1711:Number Sequence (KMP入门)
- 解决Maven项目发布报错:Publishing failed Could not publish to the server. java.lang.IndexOutOfBoundsException
- Docker 安装、操作常用命令
- 购物车Adapter recycle的item点击事件
- 使用requests的session机制模拟登陆知乎
- 字符串的getBytes方法
- java加密的方式
- chrome 查看 post 请求 _xrfs 参数
- VC6.0 cannot find the definition (implementation) of this function解决方法
- Android Greendao+多线程断点续传(方法一)
- 进制转换C语言
- 基于STC89c52单片机实现8*8led心跳效果
- SpringBoot学习6之Logback日志导入mongodb