python实现百度、CSDN、淘宝、人人自动登录(第一季)

社区账号太多,每家还要搞什么连续登陆天数,领积分啥的,经常不是忘记,就是没时间。正好在学习python,整网络爬虫,所以就尝试着写一个脚本,自动解决。新手的悲剧啊,摸索着前进,其中的艰辛无以言表啊。最后结果如下:

                     登陆                    领积分

百度空间: 成功                      失败

CSDN:      成功                      失败

淘宝:        成功                      失败

人人:        成功                       无

以后再慢慢研究吧,累死哥了!

 

代码华丽丽登场:

# -*- coding: utf-8 -*- 
import re 
import urllib 
import urllib2 
import cookielib 
import datetime
import time
from urllib2 import URLError,HTTPError
def baidu(username,password):
    #模拟登录 
    cj = cookielib.CookieJar()
    #用户名和密码 
    post_data = urllib.urlencode({'username': username, 'password': password})   
    #登录路径 
    path = 'https://passport.baidu.com/?login' 
    
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) 
    opener.addheaders = [('User-agent', 'Opera/9.23')] 
    urllib2.install_opener(opener) 
    req = urllib2.Request(path, post_data) 
    conn = urllib2.urlopen(req)
    '''
    f = open('baidu.html','w')
    print >>f,conn.read()
    f.close()
    '''
    try:
        conn = urllib2.urlopen('http://hi.baidu.com/'+username+'/ihome')
    except URLError,e:
        print >>logfile,'URLError:',e.code
        return
    except HTTPError,e:    
        logfile.wite('HTTP Error:'+e.reason)
        return
    result = conn.read()
    if('对不起您没有访问该部分内容的权限' in result):
        logfile.wite('Login failed/r/n')
    else:
        logfile.wrie('Login sucessed/r/n')
#--------------------------------------------------------------------
def csdn(logfile,username,password):
    
    logfile.write(str(datetime.datetime.now()) +' CSDN/r/n')
    #模拟登录 
    cj = cookielib.CookieJar()
    #用户名和密码 
  
    #登录路径 
    path = 'http://passport.csdn.net/ajax/accounthandler.ashx?t=log&u='+username+'&p='+password+'&c=&remember=1'#&f=http%3A//passport.csdn.net/account/login'
       
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) 
    opener.addheaders = [('User-agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13')] 
    urllib2.install_opener(opener) 
    req = urllib2.Request(path) 
    while(True):
        try:
            conn = urllib2.urlopen(req)
        except URLError,e:
            logfile.write('URLError:' +e.code + '/r/n')
            return False
        except HTTPError,e:    
            logfile.write('HTTP Error:'+e.reason+'/r/n')
            return False 
        result = conn.read()
        result = result.replace('true','True')
        result = result.replace('false','False')
        result = eval(result)
        
    
        if result['status']== True :
            logfile.write('Login sucessed/r/n')
            
        else:
            logfile.write('Login failed/r/n')
        #获取积分 
        logfile.write('Trying to gain points.... /r/n')
        try:
            conn = urllib2.urlopen('http://hi.csdn.net/cp.php?ac=task&taskid=7')
        except URLError,e:
            logfile.write('URLError:' +e.code + '/r/n')
            return False
        except HTTPError,e:    
            logfile.write('HTTP Error:'+e.reason + '/r/n')
            return False
        #判断是否成功,这里比较麻烦,因为csdn不反回成功与否,而只是上次成功获取积分距离现在的时间
        result = conn.read()
        digs = re.compile('

完成时间:.+

') r = digs.findall(result) if len(r)>0: #如果上次获取成功的时间在一小时之前,程序退出,否则每隔一秒重试一次 if ('现在' in r[0]) or ('秒' in r[0]) or ('分' in r[0]): logfile.write('Task finished/r/n') return True logfile.write('Try again after 1 second!/r/n') time.sleep(1) #break #-------------------------------------------------------------------- def renren(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: logfile.write('URLError:' +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': logfile.write('Task finished/r/n') else: logfile.write('Task failed/r/n') #-------------------------------------------------------------------- def taobao(logfile,username,password): logfile.write(str(datetime.datetime.now()) +' taobao/r/n') cj = cookielib.CookieJar() post_data = urllib.urlencode( { 'CtrlVersion': '1,0,0,7', 'TPL_password':password, 'TPL_redirect_url':'', 'TPL_username':username, '_tb_token_':'5d7eba30e3b31', 'action':'Authenticator', 'callback':'', 'css_style':'', 'event_submit_do_login':'anything', 'fc':2, 'from':'tb', 'from_encoding':'', 'guf':'', 'gvfdcname':'', 'isIgnore':'', 'llnick':'', 'loginType':3, 'longLogin':0, 'minipara' :'', 'minititle':'', 'need_sign':'', 'need_user_id':'', 'not_duplite_str':'', 'popid':'', 'poy':'', 'pstrong':'', 'sign':'', 'style':'default', 'support':'000001', 'tid':'' } ) path = 'https://login.taobao.com/member/login.jhtml' opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) opener.addheaders = [('User-agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13')] urllib2.install_opener(opener) req = urllib2.Request(path,post_data) req.addheader = ('Host', 'login.taobao.com') req.addheader = ('Referer', 'https://login.taobao.com/member/login.jhtml') #req.addheader = ('Content-Length', 414) req.addheader = ('Content-Type', 'application/x-www-form-urlencoded') #try login try: conn = urllib2.urlopen(req) except URLError,e: logfile.write('URLError:' +e.code + '/r/n') return False except HTTPError,e: logfile.write('HTTP Error:'+e.reason + '/r/n') return False #以下是尝试领取每日淘金币,最后没成功,哪位朋友有方法,希望赐教啊 #open tao jianhu,get currently coin try: conn = urllib2.urlopen('http://qz.jianghu.taobao.com/home/award_bid_home.htm?tracelog=jhleftmenu') except URLError,e: logfile.write('URLError:' +e.code + '/r/n') return False except HTTPError,e: logfile.write('HTTP Error:'+e.reason + '/r/n') return False html = conn.read() #seach my coin digs = re.compile('/d*') result = digs.findall(html) result = result[0].split('>') #my currently coin original_coin = result[1] #get get_coin_path digs = re.compile('"J_EnterTime" type="hidden" value=".*"') result = digs.findall(html) result = result[0].split('"') enter_time = result[5] #print enter_time digs = re.compile('_tb_token_.*value=.*/'') result = digs.findall(html) result = result[0].split('/'') tb_token = result[4] get_coin_path = 'http://qz.jianghu.taobao.com/home/grant_everyday_coin.htm?t=1295486909134&_tb_token=5615ee3a50637'#+tb_token #try get more coin post_data = urllib.urlencode( {'enter_time':'1295486851783',#enter_time, 'ran':'0.9395819079633437', 'user_action':'tm=MTI5NTQ4Njg2MDM1OA==|ma=0rhVdv6+FAg=|br=jVF4vau4JmjnhXPejAYmduDU/IYjoBuD|pr=0rhVdv6+FAg=|id=0rhVdv6+FAg=|mc=BKZr5rSMYFoIFxHkfHzSFA==', }) req = urllib2.Request(path,post_data) req.addheader = ('Host', 'qz.jianghu.taobao.com') req.addheader = ('Referer', 'http://qz.jianghu.taobao.com/home/award_bid_home.htm?tracelog=jhleftmenu') try: conn = urllib2.urlopen(req) except URLError,e: logfile.write('URLError:' +e.code + '/r/n') return False except HTTPError,e: logfile.write('HTTP Error:'+e.reason + '/r/n') return False #update currently coin try: conn = urllib2.urlopen('http://qz.jianghu.taobao.com/home/award_bid_home.htm?tracelog=jhleftmenu') except URLError,e: logfile.write('URLError:' +e.code + '/r/n') return False except HTTPError,e: logfile.write('HTTP Error:'+e.reason + '/r/n') return False result = conn.read() #seach my coin digs = re.compile('/d*') result = digs.findall(result) result = result[0].split('>') #my currently coin now_coin = result[1] logfile.write('Your coin:'+original_coin+'->'+ now_coin+'/r/n') #-------------------------------------------------------------------- if '__name__' == '__main__' : logfile = open('log.txt','a') renren(logfile,'','') csdn(logfile,'','') taobao(logfile,'','') logfile.close() print 'OK'


你可能感兴趣的:(Python)