Python Cookie操作实战---requests&selenium&urllib2
2015-10-26 09:30
621 查看
selenium&request 操作cookie 验证
使用requests方法单独操作cookie,应用范围 web模拟器(即不通过页面操作,直接通过http协议方式进行测试):
urllib2取cookie值
#coding:utf-8 import requests from selenium import webdriver
#启动浏览器 driver = webdriver.Chrome() driver.get(url) #获取浏览器coockie print "old cookies %s:" %(driver.get_cookie('ASP.NET_SessionId')) #清除cookie driver.delete_all_cookies() print "after delete cookies %s:" %(driver.get_cookies()) #建立request Session session = requests.Session() headers = {'Content-Type':'application/x-www-form-urlencoded'} page1 = session.get(url,headers=headers) #获取cookie值 __cookievalue = page1.cookies.get('ASP.NET_SessionId') print __cookievalue #替换cookie值 driver.add_cookie({'name':'ASP.NET_SessionId','value':__cookievalue}) print "new cookies %s" %(driver.get_cookie('ASP.NET_SessionId')) page2 = session.post(url,login_data,headers=headers) driver.get(directoUrl) print "page2 cookie %s" %(page2.cookies)
使用requests方法单独操作cookie,应用范围 web模拟器(即不通过页面操作,直接通过http协议方式进行测试):
#coding:utf-8 import requests import json import time if __name__ == '__main__': Headers = {'Content-Type':'application/json'} r = requests.post(URL,json = DATA,headers = Headers ) print r.cookies #设置cookie cookie = "xxx1=%s;xxx2=%s,xxx3=%s" %(r.cookies.get('xxx1'),r.cookies.get('xxx2'),r.cookies.get('xxx3'))
urllib2取cookie值
import urllib2 import cookielib cookie = cookielib.CookieJar() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie)) response = opener.open('http://www.google.com') for item in cookie: if item.name == 'some_cookie_item_name': print item.value
相关文章推荐
- Ubuntu下一个python的BeautifulSoup和rsa安装方法---信息检索project2部分:微博爬行要求python包裹
- 笨办法学python的笔记之八(ex42 class类)
- (3)python运算符
- python 小记
- Python3学习笔记02
- Python3 socket编程,并与多线程实现最简单的聊天工具之一
- python学习记录之_1025
- 深入讲解Python中的迭代器和生成器
- Windows下用PyCharm和Visual Studio开始Python编程
- 以Python代码实例展示kNN算法的实际运用
- caffe 练习4 ----利用python批量抽取caffe计算得到的特征------by 香蕉麦乐迪
- python学习——基础(八)
- 值得关注的10个python语言博客(转)
- day1 python学习随笔
- Python获取并修改hosts
- 详解python2 和 python3的区别
- Python监控网站运行状况
- 用Python写一个猜数字小游戏
- Python框架 Flask 项目实战教程
- 使用python的Flask实现一个RESTful API服务器端[翻译]