本文实例讲述了python人人网登录应用的实现方法,分享给大家供大家参考。
具体方法如下:
import re import urllib import urllib2 import cookielib import datetimeimport timefrom 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程序设计有所帮助。
新闻热点
疑难解答
图片精选