本文实例讲述了python人人网登录应用的实现方法,分享给大家供大家参考。
具体方法如下:
import re
import urllib
import urllib2
import cookielib
import datetime
import time
from urllib2 import urlerror,httperror
#第一个参数为日志文件,第二个参数为用户名,第三个参数为密码
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’)#http://www.renren.com/syshome.do
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’)
return conn.read()
else:
print ‘task failed’
logfile.write(‘task failed/r/n’)
# 登录成功,返回整个页面的代码
file_object = open(“log.txt”,’w’)
login_index = renren_login(file_object,’xxxxxx’,’xxxx’)
希望本文所述对大家的python程序设计有所帮助。