Python模拟登陆(使用requests库)
来源:互联网 发布:mac os 文件管理 编辑:程序博客网 时间:2024/04/27 01:29
之前使用 Python自带urllib2库做了一个简单的模拟登陆 人人网 的功能。发现很多网站都比人人网的登陆流程复杂的多,需要好好抓包研究每一步请求过程及需要的Cookie,urllib2的cookie处理器就有点相形见绌了。很多都需要自己实现。
而由于urllib2本身的使用起来不方便,所以改用第三方库requests库,这个库也是使用很广泛的一个Python库。
当然了,每个网站的登陆流程略有不同,代码不可能完全通用,需要自己抓包分析过程,(使用HTTPFox等),弄懂原理,然后稍作修改。
# -*- coding:utf-8 -*-import urllibimport sslimport requestsg_cookie = ''def login_web():url = 'https://www.xxx.com'#verify和allow_redirects看需要,我这边需要取到每一步的cookie所以禁用重定向。前面的get请求一般都是为了获取Cookieresp = requests.get(url, verify=False, allow_redirects=False)g_cookie = resp.headers['set-cookie']get_url = url + resp.headers['location']headers = {'Cookie':g_cookie}resp = requests.get(get_url,headers=headers,verify=False, allow_redirects=False)#print "resp.headers:",resp.headersget_url = resp.headers['location']resp = requests.get(get_url,verify=False, allow_redirects=False)#print "===>",resp.headerspost_url = resp.headers['location']headers = {'Content-type':'application/x-www-form-urlencoded'}data = {'username':'test','password':'password','other':'other'}#这里才是真正的登陆过程,data里面是抓包获取的账号密码及其他信息。resp = requests.post(post_url, urllib.urlencode(data),headers=headers,verify=False,allow_redirects=False)get_url = resp.headers['location']headers = {'Cookie':resp.headers['set-cookie']}#我要登陆的网站重定向比较多,所以不停的get。r = requests.get(get_url,headers=headers,verify=False,allow_redirects=False)get_url= r.headers['location']headers = {'Cookie':g_cookie} # 最开始获得的Cookie派上用场了resp = requests.get(get_url,headers=headers,verify=False,allow_redirects=False)get_url = 'https://www.xxx.com'resp = requests.get(get_url, headers=headers,verify=False,allow_redirects=False)file = open('login_info.txt','w')file.write(resp.content)if __name__ == '__main__':login_web()
0 0
- Python模拟登陆(使用requests库)
- 使用 python requests 模拟登陆
- 使用Python的Requests包模拟登陆
- Python requests 模拟登陆
- python requests模拟登陆github
- 使用requests模拟登陆github
- 使用requests模拟登陆csdn
- Python使用requests库模拟登陆网站的方式--以豆瓣为例
- Python使用requests库模拟登陆网站的方式--以豆瓣为例
- 利用Python requests库模拟登陆学校教务系统
- 利用python requests库模拟登陆知乎
- 【LeetCode】Python requests模拟Leetcode登陆
- Python使用Requests第三方库自动登陆知乎
- requests模拟登陆
- 【python爬虫01】使用requests库模拟登录知乎
- python模拟登陆知乎和CSDN【urllib,re,requests】
- python requests模拟登陆带验证码的网站
- 使用requests模拟登陆的三种方式
- VisualStudio2010配置OpenCV的一种一劳永逸的方法
- codeforces 593B
- Dynamics CRM 自定义上传附件的图片悬浮层显示
- resin的配置【转】
- 【原创】Logistic regression (逻辑回归) 概述
- Python模拟登陆(使用requests库)
- linux 永久路由配置
- Android使用特殊权限的一种代码实现
- 上传到hdfs文件所属问题
- struts2+spring的两种整合方式
- equals与==的区别
- 2009-08-11-有缘即住无缘去,一任清风送白云
- zabbix邮件报警设置
- 华为OJ基础题-字符串运用-密码截取