python登陆网页并处理网站session和cookie

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

这是一个python通过urllib直接登陆网站,并处理网站的session和cookie

import cookielib, urllib, urllib2
 
login = 'ismellbacon123@yahoo.com'
password = 'login'
 
# Enable cookie support for urllib2
cookiejar = cookielib.CookieJar()
urlOpener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))
 
# Send login/password to the site and get the session cookie
values = {'login':login, 'password':password }
data = urllib.urlencode(values)
request = urllib2.Request("http://www.imdb.com/register/login", data)
url = urlOpener.open(request)  # Our cookiejar automatically receives the cookies
page = url.read(500000)
 
# Make sure we are logged in by checking the presence of the cookie "id".
# (which is the cookie containing the session identifier.)
if not 'id' in [cookie.name for cookie in cookiejar]:
    raise ValueError, "Login failed with login=%s, password=%s" % (login,password)
 
print "We are logged in !"
 
# Make another request with our session cookie
# (Our urlOpener automatically uses cookies from our cookiejar)
url = urlOpener.open('http://imdb.com/find?s=all&q=grave')
page = url.read(200000)