Python cookielib login to Renren

Source: Internet
Author: User

This evening is not very busy, so I started to write something on the computer early. I want to share with you one of my ownPythonThe script for automatically logging on to Renren is no way to be lazy! Enter the account and password lazily, so that python can reduce the workload! Let's start with the script and talk about the knowledge points later:

#! /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 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 ( '<H3> (.*?) </H3>' ) # Matching range
Apatten = Re . Compile ( '<A. +> (. +) </a> :' ) # Matching author
Cpatten = Re . Compile ( '</A> (. +) \ s' ) # Matching 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 address
Origurl = 'Http: // www.renren.com/home.do' # Address after Renren Logon
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 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 before you can log on. If you run this script, you will find that there isProgramA Renren. Cookie file is automatically created.

I
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 use cookies to log on to a website, you must use the cookielib module. Otherwise, you cannot log on to the website using a program, an example of writing a urlib during the disconnection time is provided. You can
use the above script to play with it! Experience the fun of Python Renren Code ! If you are interested in the Web, you can see: Use Python beautifulsoup to parse web pages, Python beautifulsoup multi-thread analysis to capture web pages

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.