Let's start with the script and talk about the knowledge points later:
Copy codeThe Code is as follows :#! /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 = ''# login operation object
Self. cj = cookielib. LWPCookieJar ()
Try:
Self. cj. revert ('renren. coockie ')
Except t 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 User Logon information '''
Self. name = username
Self. pwd = password
Self. domain = domain
Self. origURL = origURL
Def login (self ):
'''Log on to Renren net '''
Params = {'domain ': self. domain, 'url url': 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 ):
'''View the Update Status of a friend '''
Self. _ caiinfo ()
Def _ caiinfo (self ):
'''Collection information '''
H3patten = re. compile ('Apatten = re. compile ('<a. +> (. +) </a>:') # match the author
Cpatten = re. compile ('</a> (. +) \ s') # match 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], 'says:', info [0]
Print '----------------------------------------------'
Else:
Continue
Ren = Renren ()
Username = ''# Your Renren account
Password = ''# Your Renren password
Domain = 'renren. com' # renren address
OrigURL = 'HTTP: // The address behind www.renren.com/home.do'renren.com
Ren. setinfo (username, password, domain, origURL)
Ren. login ()
The python cookielib, urllib2, and urllib modules are mainly used. These three modules are python's better http modules.
Self. cj = cookielib. LWPCookieJar ()
Try:
Self. cj. revert ('renren. coockie ')
Except t Exception, e:
Print e
Self. opener = urllib2.build _ opener (urllib2.HTTPCookieProcessor (self. cj ))
Urllib2.install _ opener (self. opener)
These rows are cookies for the local Renren network, because Renren needs to verify cookies to log on. If you run this script, a program in the current directory will automatically create a renren. cookie.
Renren. cookie Information is: # LWP-Cookies-2.0 Set-Cookie3: WebOnLineNotice_244225225 = 1; path = "/"; domain = ".renren.com"; path_spec; domain_dot; expires = "2010-04-11 06: 59: 33Z "; version = 0. To sum up, if you want to use cookies for website login, you need to use the cookielib module. Otherwise, you cannot log on to the website using a program, and you need to write an urlib example during the expiration time, you can use the script above for fun! Experience the fun of python Renren code!