python 的cookie处理操作

本文详细介绍了如何使用Python的cookielib和urllib2模块来访问网站并利用Cookie进行身份验证。包括如何从网站获取Cookie,将其保存到文件中,以及如何使用指定参数生成Cookie并用于后续访问。通过实例代码演示了访问不同页面的过程,确保了登录状态的保持。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 使用已有的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中,我们可以使用`requests`库来处理HTTP请求,包括cookies。如果你想要发送HTTP请求并管理cookies,可以按照以下步骤操作: 1. **安装requests库**: 如果还没有安装`requests`,可以使用pip进行安装: ``` pip install requests ``` 2. **获取或设置cookies**: 使用`requests.get()`或`requests.post()`等函数发送请求时,cookies作为参数传递给`cookies`选项。例如,发送GET请求并接受cookies: ```python import requests response = requests.get('https://example.com', cookies={'key': 'value'}) ``` 或者,你可以创建一个`CookieJar`对象来存储cookies: ```python from requests.cookies import CookieJar jar = CookieJar() # 添加cookie jar.set('key', 'value', domain='example.com') # 使用jar发送请求 response = requests.get('https://example.com', cookies=jar) ``` 3. **读取和更新cookies**: 可以通过`response.cookies`属性获取响应返回的所有cookies,然后根据需要进行更新: ```python for cookie in response.cookies: print(f'{cookie.name}={cookie.value}') # 更新cookie jar['new_cookie_name'] = 'new_value' ``` 4. **持久化cookies**: 对于某些网站,你可能希望在多次会话中保持登录状态。在这种情况下,可以将`CookieJar`保存到文件中,下次请求时从文件加载: ```python with open('cookies.txt', 'wb') as f: f.write(jar.dump()) ``` 5. **清除cookies**: 当你想删除特定的cookie时,可以用以下方法: ```python jar.clear(domain='example.com', name='key') ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值