模板方法
python也是一种面向对象的语言,所以在实现群发的时候,会登陆不同的网站,但是登陆的方法什么的不尽相同,所以这里想到的是模板方法。
模板方法模式:
应用特性:重复做相同逻辑的事情,但是具体细节不同的场景
结构特性:相同逻辑抽取至父类,具体细节留置子类。可以说是对逻辑的抽象
看一下代码:
#!/usr/bin/env python #encoding: utf-8 class template: def __init__(self): pass def logic(self): print 'do something before ....' print self.do_something_now() print 'do something after ....' def do_something_now(self): return None class apply_temp1(template): def __init__(self): pass def do_something_now(self): return 'apply 1' class apply_temp2(template): def __init__(self): pass def do_something_now(self): return 'apply 2' if '__main__' == __name__: obj1 = apply_temp1() obj2 = apply_temp2() obj1.logic() obj2.logic() print obj1.__class__ print obj2.__class__
得到结果如下:
然后看一下类图:
是不是很简单。
baidu登陆流程
想实现登陆baidu,使用firefox查看,可以看到如下图:
baidu HI登陆
baidu HI登陆源代码
# _*_ coding:utf-8 _*_ # name login_baidu.py import urllib,urllib2,httplib,cookielib def auto_login_hi(url,name,pwd): url_hi="http://passport.baidu.com/?login" #设置cookie cookie=cookielib.CookieJar() cj=urllib2.HTTPCookieProcessor(cookie) #设置登录参数 postdata=urllib.urlencode({'username':name,'password':pwd}) #生成请求 request=urllib2.Request(url_hi,postdata) #登录百度 opener=urllib2.build_opener(cj) f=opener.open(request) if(200==f.getcode()): print "登陆成功!" else: print "登录失败!" #print f.getcode() #打开百度HI空间页面 hi_html=opener.open(url) return hi_html if __name__=='__main__': name='用户名' password='密码' url='http://hi.baidu.com/ewayfly' h=auto_login_hi(url,name,password) print h.read()
登陆博客园
登录博客园的代码:
# _*_ coding:utf-8 _*_ import urllib,urllib2,httplib,cookielib def auto_login_cnblogs(url,name,pwd): url_hi="http://passport.cnblogs.com/login.aspx?ReturnUrl=http%3A%2F%2Fwww.cnblogs.com%2F" #设置cookie cookie=cookielib.CookieJar() cj=urllib2.HTTPCookieProcessor(cookie) #设置登录参数 postdata=urllib.urlencode({'username':name,'password':pwd}) #生成请求 request=urllib2.Request(url_hi,postdata) #登录百度 opener=urllib2.build_opener(cj) f=opener.open(request) if(200==f.getcode()): print "登陆成功!" else: print "登录失败!" #print f.getcode() hi_html=opener.open(url) return hi_html if __name__=='__main__': name='用户名' password='密码' url='http://www.cnblogs.com/skyme/' h=auto_login_cnblogs(url,name,password) print h.read()
登陆51CTO
登陆51CTO:
#coding:UTF-8 import urllib,urllib2,cookielib,re,random class Login: _login_url = 'http://home.51cto.com/index.php?s=/Index/doLogin' _method = 'post' #email 51cto登录用户名或邮箱 #passwd 51cto登录密码 _login_data = { 'email':'用户名',\ 'passwd':'密码',\ } _headers = [ ('host','home.51cto.com'),\ ('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2'),\ ('Referer','http://home.51cto.com/index.php?s=/Index/index/reback/http%253A%252F%252Fwww.51cto.com%252F/')\ ] _data = { 'cookie_file_path':'./51cto_cookies.dat' } _re = r'src="(.+?)"' _version = '0.1' _connect_info = {} def __init__(self): self._connect_info['cookie'] = cookielib.LWPCookieJar() try: self._connect_info['cookie'].revert(self._data['cookie_file_path']) except Exception,e: print e self._connect_info['cookie_processor'] = urllib2.HTTPCookieProcessor(self._connect_info['cookie']) self._connect_info['post_data'] = urllib.urlencode(self._login_data) def open(self): opener = urllib2.build_opener(self._connect_info['cookie_processor']) opener.addheaders = self._headers urllib2.install_opener(opener) #opener.open(request) request = urllib2.Request(self._login_url,self._connect_info['post_data']) conn = opener.open(request) if(conn.geturl() == self._login_url): self._connect_info['cookie'].save(self._data['cookie_file_path']) else: pass #根据js中的链接连接登录 partner = re.compile(self._re) match = partner.findall(conn.read()) for item in match: opener.open(item) #登录成功开始领豆 url = 'http://down.51cto.com/download.php' data = {'do':'getfreecredits','t':random.random()} login51cto = opener.open(url, urllib.urlencode(data)) print login51cto.getcode() #html = opener.open('http://down.51cto.com/') #领无忧币 url = 'http://home.51cto.com/index.php?s=/Home/toSign' data = {'s':''} loginwuyou = opener.open(url, urllib.urlencode(data)) print loginwuyou.getcode() if __name__ == '__main__': login_51cto = Login() login_51cto.open()