什么是 cookie:某些网站为了辨别用户身份,进行 session 跟踪而存储在用户本地上的数据,通过 cookie 可以访问网站的其他页面,否则是禁止访问的
如何获取 cookie :
In [1]: import urllib2, cookielib In [2]: cookie = cookielib.CookieJar() # 获取存在本机的cookie消息,但第一次获取为空,只有访问了一个URL之后才有cookie信息 In [3]: handler = urllib2.HTTPCookieProcessor(cookie) # 创建一个处理器,用于处理cookie信息 In [4]: opener = urllib2.build_opener(handler) # 通过cookie处理器构建opener,使得opener跟CookieJar对象绑定 In [5]: response = opener.open(‘http://www.baidu.com/‘) # 使用opener来访问一个URL便会收集到cookie信息 In [6]: for i in cookie: ....: print ‘%s: %s‘ % (i.name, i.value) ....: BAIDUID: D9487EEE0A9D16C06CA7517EACC00798:FG=1 BIDUPSID: D9487EEE0A9D16C06CA7517EACC00798 H_PS_PSSID: 1445_21112_18560_17001_25178_20930 PSTM: 1514119274 BDSVRTM: 0 BD_HOME: 0
实例:通过 cookie 自动登录企业邮箱
#!/usr/bin/env python #-*- coding: utf8 -*- import re import urllib import urllib2 import cookielib class MyMail(object): def __init__(self): self.headers = {‘User-Agent‘: ‘Mozilla/5.0 (Windows NT6.1; rv:47.0) Gecko/20100101 Firefox/47.0‘} self.baseURL = ‘https://bjmail.clustertech.com/roundcude/‘ self.loginURL = self.baseURL + ‘?_task=login‘ def getPage(self): self.cookie = cookielib.CookieJar() handler = urllib2.HTTPCookieProcessor(self.cookie) self.opener = urllib2.build_opener(handler) try: request = urllib2.Request(url=self.baseURL, headers=self.headers) response = self.opener.open(request) return response.read() except urllib2.URLError, e: if hasattr(e, ‘reason‘): print "连接服务器失败,错误原因", e.reason return None def getToken(self): page = self.getPage() re_token = re.compile(r‘<form.*?<input.*?value="(.*?)".*?>‘, re.S) token = re_token.search(page) if token: return token.group(1) else: return None def login(self): data = {"_token":self.getToken(), "_task":"login", "_action":"login", "_timezone":"Asia/Shanghai", "_url":"", "_user":"zyli", "_pass":"1234.qwe"} self.postdata = urllib.urlencode(data) try: request = urllib2.Request(url = self.loginURL, data = self.postdata, headers = self.headers) response = self.opener.open(request) print response.read() except urllib2.URLError, e: print e gs = MyMail() gs.getToken() gs.login()