cookielib库 和 HTTPCookieProcessor处理器
1. 获取Cookie,并保存到CookieJar()对象中
import urllib2
import cookielib
cookiejar = cookielib.CookieJar()
handler = urllib2.HTTPCookieProcessor(cookiejar)
opener = urllib2.build_opener(handler)
opener.open("http://www.baidu.com")
cookieStr = ""
for item in cookiejar:
cookieStr = cookieStr + item.name + "=" + item.value + ";"
print cookieStr[:-1]
2. 访问网站获得cookie,并把获得的cookie保存在cookie文件中
import cookielib
import urllib2
filename = "cookie.txt"
cookiejar = cookielib.MozillaCookieJar(filename)
handler = urllib2.HTTPCookieProcessor(cookiejar)
opener = urllib2.build_opener(handler)
response = opener.open("http://www.baidu.com")
cookiejar.save()
3. 从文件中获取cookies,做为请求的一部分去访问
import cookielib
import urllib2
cookiejar = cookielib.MozillaCookieJar()
cookiejar.load('cookie.txt')
handler = urllib2.HTTPCookieProcessor(cookiejar)
opener = urllib2.build_opener(handler)
print opener.open("http://www.baidu.com")
利用cookielib和post登录人人网
import urllib
import urllib2
import cookielib
cookie = cookielib.CookieJar()
cookie_handler = urllib2.HTTPCookieProcessor(cookie)
opener = urllib2.build_opener(cookie_handler)
opener.addheaders = [("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36")]
data = {"email":"", "password":""}
postdata = urllib.urlencode(data)
request = urllib2.Request("http://www.renren.com/PLogin.do", data = postdata)
opener.open(request)
response = opener.open("http://www.renren.com/3054519653/profile")
print response.read()