本文实例讲述了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 = '/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')#/SysHome.do
return False
except HTTPError,e:
logfile.write('HTTP Error:'+e.reason + '/r/n')
return False
if conn.geturl() == '/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程序设计有所帮助。
如果觉得《python应用实例论文_python人人网登录应用实例》对你有帮助,请点赞、收藏,并留下你的观点哦!