Python renren login application instance

  • 2020-04-02 14:10:58
  • OfStack

This article is an example of python renren login application implementation method, to share for your reference.

Specific methods are as follows:


import re 
import urllib 
import urllib2 
import cookielib 
import datetime
import time
from urllib2 import URLError,HTTPError
# The first parameter is the log file, the second parameter is the username, and the third parameter is the 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':password,
    }
    )
  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.write('URLError:' + 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 code for the entire page     
file_object = open("log.txt",'w')    
login_index = renren_login(file_object,'xxxxxx','xxxx')

I hope this article has helped you with your Python programming.


Related articles: