Python implements automatic login to renren and access recent visitor instances

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

This article illustrates the python implementation of automatic login renren.com and access to the recent visitors method, to share with you for your reference.

Specific methods are as follows:


##-*- coding : gbk -*- 
# in  
import os 
from xml.dom import minidom  
import re  
import urllib  
import urllib2  
import cookielib  
import datetime 
import time 
from urllib2 import URLError,HTTPError 
# Login module on the web  
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') 
    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') 
    open('login_renren.html','w').write(conn.read()) 
    return conn.read() 
  else: 
    print 'Task Failed' 
    logfile.write('Task failed/r/n') 
# The generated logon log file      
file_object = open("log.txt",'w')     
login_index = renren_login(file_object,' The user name ',' password ') # Change this place to the login username and password   
 
#parse Parse web pages  
r1 = re.compile('''''http://www.renren.com/profile.do?portal=homeFootprint&ref=home_footprint&id=d{9}''') 
li = r1.findall(open("login_renren.html","r").read()) 
# Put the address of the most recent visit resolved into the dictionary 
url_dict ={}for item in li: print item url_dict.setdefault(item) 
# Visit a recent visitor 
for item in url_dict.iterkeys(): os.startfile(item) 

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


Related articles: