本文實例講述了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程序設計有所幫助。
新聞熱點
疑難解答