博客群發(2)--實現登陸


模板方法

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__

得到結果如下:

image

然后看一下類圖:

模板

是不是很簡單。

baidu登陸流程

想實現登陸baidu,使用firefox查看,可以看到如下圖:

image

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()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM