[Python] python登陆网页并处理网站session和cookie →→→→→进入此内容的聊天室

来自 , 2020-11-23, 写在 Python, 查看 133 次.
URL http://www.code666.cn/view/2c3279b0
  1. import cookielib, urllib, urllib2
  2.  
  3. login = 'ismellbacon123@yahoo.com'
  4. password = 'login'
  5.  
  6. # Enable cookie support for urllib2
  7. cookiejar = cookielib.CookieJar()
  8. urlOpener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))
  9.  
  10. # Send login/password to the site and get the session cookie
  11. values = {'login':login, 'password':password }
  12. data = urllib.urlencode(values)
  13. request = urllib2.Request("http://www.imdb.com/register/login", data)
  14. url = urlOpener.open(request)  # Our cookiejar automatically receives the cookies
  15. page = url.read(500000)
  16.  
  17. # Make sure we are logged in by checking the presence of the cookie "id".
  18. # (which is the cookie containing the session identifier.)
  19. if not 'id' in [cookie.name for cookie in cookiejar]:
  20.     raise ValueError, "Login failed with login=%s, password=%s" % (login,password)
  21.  
  22. print "We are logged in !"
  23.  
  24. # Make another request with our session cookie
  25. # (Our urlOpener automatically uses cookies from our cookiejar)
  26. url = urlOpener.open('http://imdb.com/find?s=all&q=grave')
  27. page = url.read(200000)
  28. #//python/1862

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

这儿你可以回复上面这条便签

captcha