前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >博客群发(2)--实现登陆

博客群发(2)--实现登陆

作者头像
cloudskyme
发布2018-03-20 16:08:33
6510
发布2018-03-20 16:08:33
举报
文章被收录于专栏:cloudskymecloudskyme

模板方法

python也是一种面向对象的语言,所以在实现群发的时候,会登陆不同的网站,但是登陆的方法什么的不尽相同,所以这里想到的是模板方法。

模板方法模式:

应用特性:重复做相同逻辑的事情,但是具体细节不同的场景

结构特性:相同逻辑抽取至父类,具体细节留置子类。可以说是对逻辑的抽象

看一下代码:

代码语言:javascript
复制
#!/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
image

然后看一下类图:

模板
模板

是不是很简单。

baidu登陆流程

想实现登陆baidu,使用firefox查看,可以看到如下图:

image
image

baidu HI登陆

baidu HI登陆源代码

代码语言:javascript
复制
# _*_ 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()

登陆博客园

代码语言:javascript
复制
登录博客园的代码:
代码语言:javascript
复制
# _*_ 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:

代码语言:javascript
复制
#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()
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013-11-21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 模板方法
  • baidu登陆流程
  • baidu HI登陆
  • 登陆博客园
  • 登陆51CTO
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档