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