The example of this article describes the Python Renren login application implementation method, share for everyone for reference.
Here's how:
Import re import urllib import urllib2 import cookielib import datetimeimport timefrom urllib2 import urlerror,httperror# First One parameter is the log file, the second parameter is the 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.re Nren.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.write (' U Rlerror: ' + str (e.code) + '/r/n ') #http://www.renren.com/syshome.do return False except Httperror,e:logfile.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 successful, return the entire page of code file_object = open ("Log.txt", ' w ') Login_index = Renren_login (file_object, ' xxxxxx ', ' xx xx ')
Hopefully this article will help you with Python programming.