python 的cookie处理操作
2011-07-31 22:01
281 查看
使用已有的cookie访问网站
import cookielib, urllib2
ckjar = cookielib.MozillaCookieJar(os.path.join(’C:\Documents and Settings\tom\Application Data\Mozilla\Firefox\Profiles\h5m61j1i.default’, ‘cookies.txt’))
req = urllib2.Request(url, postdata, header)
req.add_header(’User-Agent’, \
‘Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)’)
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(ckjar) )
f = opener.open(req)
htm = f.read()
f.close()
访问网站获得cookie,并把获得的cookie保存在cookie文件中
import cookielib, urllib2
req = urllib2.Request(url, postdata, header)
req.add_header(’User-Agent’, \
‘Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)’)
ckjar = cookielib.MozillaCookieJar(filename)
ckproc = urllib2.HTTPCookieProcessor(ckjar)
opener = urllib2.build_opener(ckproc)
f = opener.open(req)
htm = f.read()
f.close()
ckjar.save(ignore_discard=True, ignore_expires=True)
使用指定的参数生成cookie,并用这个cookie访问网站
import cookielib, urllib2
ckjar = cookielib.MozillaCookieJar(os.path.join(’C:\Documents and Settings\tom\Application Data\Mozilla\Firefox\Profiles\h5m61j1i.default’, ‘cookies.txt’))
req = urllib2.Request(url, postdata, header)
req.add_header(’User-Agent’, \
‘Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)’)
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(ckjar) )
f = opener.open(req)
htm = f.read()
f.close()
访问网站获得cookie,并把获得的cookie保存在cookie文件中
import cookielib, urllib2
req = urllib2.Request(url, postdata, header)
req.add_header(’User-Agent’, \
‘Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)’)
ckjar = cookielib.MozillaCookieJar(filename)
ckproc = urllib2.HTTPCookieProcessor(ckjar)
opener = urllib2.build_opener(ckproc)
f = opener.open(req)
htm = f.read()
f.close()
ckjar.save(ignore_discard=True, ignore_expires=True)
使用指定的参数生成cookie,并用这个cookie访问网站
import urllib.request as ur import urllib.parse as up #import http.cookiejar as cj #coding:gbk charset='utf8' #组件信息 #cookiejar = cj.CookieJar() #urlopener = ur.build_opener(ur.HTTPCookieProcessor(cookiejar)) urlopener = ur.build_opener(ur.HTTPCookieProcessor()) #jiayuan配置信息 name='Yatere' uid='22017518' #http头 headers={'User-Agent':'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)'} seachhead={'User-Agent':'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)', 'Referer':'http://search.jiayuan.com/', 'Origin':'http://search.jiayuan.com'} #用户登入data values = {'password':'198398','name':'yin_kai@163.com'} #搜索data searchdata={ 'sex':'f', 'work_location':'42', 'work_sublocation':'4201', 'min_age':'22', 'max_age':'26', 'min_height':'160', 'max_height':'180', 'education':'20', 'edu_more_than':'on', 'astro':'0', 'animal':'', 'bloodtype':'0', 'income':'0', 'house':'0', 'auto':'0', 'marriage':'1', 'children':'0', 'level':'0', 'industry':'0', 'company':'0', 'home_location':'42', 'home_sublocation':'4201', 'nation':'0', 'belief':'0', 'ques_love':'0', 'avatar':'on', 'save_name':''} #post数据转换程序 def data(values): data=up.urlencode(values).encode() return data #访问指定页面 def geturlcon(url,data=None,headers=headers): request = ur.Request(url,data,headers) url = urlopener.open(request) page=url.read().decode('utf8','ignore') return page #检测是否登入成功 def checklogin(page): if page.find(name)>0: return True elif page.find(uid)>0: return True else: return False #访问登陆页面(获得cookie) url1='http://login.jiayuan.com/dologin.php' geturlcon(url1,data(values),headers) #登入后访问其他页面 url2='http://www.jiayuan.com/usercp/' page=geturlcon(url2) if checklogin(page): print ('登入成功') else: print ('登入失败') url3='http://profile.jiayuan.com/14214171' page=geturlcon(url3) if checklogin(page): print (url3,'登入成功') else: print ('登入失败') #查看搜索结果 url3='http://search.jiayuan.com/result.php?t=10&m=1' page=geturlcon(url3,data(searchdata),seachhead) if checklogin(page): print (url3,'登入成功') else: print ('登入失败')
相关文章推荐
- Python的Django框架中的Cookie相关处理
- Python异常处理操作实例详解
- 关于Python操作excel过程中,将多个内容写到同一个excel的不同sheet页的处理
- python计算机视觉编程——第一章(基本的图像操作和处理)
- Python-Image 基本的图像处理操作
- Python 自动登录网站(处理Cookie)
- Python中Cookie的处理(二)cookielib库
- Python脚本控制的WebDriver 常用操作 <十六> 处理对话框
- Mac 下安装 Python-OpenCV Python-OpenCV 处理图像(一):基本操作
- Python利用CookieJar自动处理Cookies
- python操作的处理
- Python的cookie处理分享
- Python 读取键盘输入字符,多线程操作,文件操作随机处理 开发范例
- Python3与OpenCV3.3 图像处理(二十四)--开闭操作
- Python脚本控制的WebDriver 常用操作 <十五> 处理Navigation Bar
- python对cookie的操作模块-cookielib
- Python-OpenCV 处理图像(二)(三):滤镜和图像运算 图像像素点操作
- Python 字符串处理操作
- Python Cookie HTTP获取cookie并处理
- Python requests 库的 cookie 处理