This article describes the Python Renren login application of the implementation method, share for everyone to reference.
The specific methods are as follows:
Import re import urllib import urllib2 import cookielib import datetime import time from URLLIB2 import urlerror,http Error #第一个参数为日志文件, the second parameter is user name, and the third parameter is password def renren_login (Logfile,username,password): Logfile.write (str ( Datetime.datetime.now ()) + ' renren/r/n ') CJ = Cookielib. Cookiejar () Post_data = Urllib.urlencode ({' Email ': username, ' password ':p assword,}) path = ' http:// Www.renren.com/PLogin.do ' opener = Urllib2.build_opener (urllib2. Httpcookieprocessor (CJ)) Urllib2.install_opener (opener) req = Urllib2. Request (path,post_data) Try:conn = Urllib2.urlopen (req) except Urlerror,e:print ' Urlerror ' LOGFILE.WR Ite (' Urlerror: ' + str (e.code) + '/r/n ') #http://www.renren.com/syshome.do return False except HTTPERROR,E:LOGF Ile.write (' HTTP Error: ' +e.reason + '/r/n ') return False if conn.geturl () = = ' Http://www.renren.com/home ': print ' Success ' Logfile.write (' Task finished/r/n ') return Conn.read () else:print ' Task Failed ' logfile.write (' Task failed/r/n ') # login succeeded, return the entire page code file_object = open ("Log.txt", ' W ') Login_index
= Renren_login (File_object, ' xxxxxx ', ' xxxx ')
I hope this article will help you with your Python programming.