Python cookielib implementation code for logging on to Renren

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.