Python cookielib login renren implementation code

  • 2020-04-02 09:47:09
  • OfStack

First on the script, and so on the next point:

 
#!/usr/bin/env python 
#encoding=utf-8 
import sys 
import re 
import urllib2 
import urllib 
import cookielib 

class Renren(object): 

def __init__(self): 
self.name = self.pwd = self.content = self.domain = self.origURL = '' 
self.operate = ''# The operation object logged in  
self.cj = cookielib.LWPCookieJar() 
try: 
self.cj.revert('renren.coockie') 
except Exception,e: 
print e 

self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj)) 
urllib2.install_opener(self.opener) 


def setinfo(self,username,password,domain,origURL): 
''' Set the user login information ''' 
self.name = username 
self.pwd = password 
self.domain = domain 
self.origURL = origURL 

def login(self): 
''' Login to renren ''' 
params = {'domain':self.domain,'origURL':self.origURL,'email':self.name, 'password':self.pwd} 
print 'login.......' 
req = urllib2.Request( 
'http://www.renren.com/PLogin.do', 
urllib.urlencode(params) 
) 

self.operate = self.opener.open(req) 

if self.operate.geturl() == 'http://www.renren.com/Home.do': 
print 'Logged on successfully!' 
self.cj.save('renren.coockie') 
self.__viewnewinfo() 
else: 
print 'Logged on error' 

def __viewnewinfo(self): 
''' Check your friends' status updates ''' 
self.__caiinfo() 


def __caiinfo(self): 
''' To collect information ''' 

h3patten = re.compile('<h3>(.*?)</h3>')# Match the range  
apatten = re.compile('<a.+>(.+)</a>:')# Match the author  
cpatten = re.compile('</a>(.+)s')# Match the content  
infocontent = self.operate.readlines() 
# print infocontent 
print 'friend newinfo:' 
for i in infocontent: 
content = h3patten.findall(i) 
if len(content) != 0: 
for m in content: 
username = apatten.findall(m) 
info = cpatten.findall(m) 
if len(username) !=0: 
print username[0],' said :',info[0] 
print '----------------------------------------------' 
else: 
continue 

ren = Renren() 
username = ''# Your renren account  
password = ''# Your renren password  
domain = 'renren.com'# Renren's address  
origURL = 'http://www.renren.com/Home.do'# Renren after the login address  
ren.setinfo(username,password,domain,origURL) 
ren.login() 

Mainly used python cookielib,urllib2,urllib these three modules, these three modules are python to do HTTP better modules.


Self. Cj = cookielib. LWPCookieJar ()

Try:
Self. Cj. Revert (' renren. Cookies')
Except the Exception, e:
Print e
The self. The opener = urllib2. Build_opener (urllib2 HTTPCookieProcessor (self. Cj))

Urllib2. Install_opener (self. Opener)
These lines are setting up cookies for renren locally, because renren needs to verify cookies in order to log in. If you run this script, you will find that a program in the current directory will automatically set up a renren.cookie file.

The information of renren.cookie here is: # lwp-cookie-2.0 set-cookie3: WebOnLineNotice_244225225=1; Path = "/" Domain = "renren.com"; Path_spec; Domain_dot; Expires = "06:59:33 2010-04-11 Z"; Version =0 summary if the website login to use cookies, it is necessary to use the cookielib module, or you can not use the program login site, too long in the urlib example, you can use the above script to play! Experience the fun of python renren code!


Related articles: