本文实例讲述了python实现自动登录人人网并访问最近来访者的方法,分享给大家供大家参考。
具体方法如下:
##-*- coding : gbk -*- #在 import os from xml.dom import minidom 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') 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') open('login_renren.html','w').write(conn.read()) 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,'用户名','密码') #这地方换成登录的用户名和密码 #parse 解析网页 r1 = re.compile('''''http://www.renren.com/profile.do/?portal=homeFootprint&ref=home_footprint&id=/d{9}''') li = r1.findall(open("login_renren.html","r").read()) #将解析到的最近来访的地址放入字典中url_dict ={}for item in li: print item url_dict.setdefault(item) #访问最近来访者for item in url_dict.iterkeys(): os.startfile(item)
希望本文所述对大家的Python程序设计有所帮助。
新闻热点
疑难解答