python寫了個登陸moofeel網站回複文章並領取魔幣的程式

來源:互聯網
上載者:User
#!/usr/bin/env python#encoding=utf-8import sys,re,urllib2,urllib,cookielib,chardet,timefrom BeautifulSoup import BeautifulSoupreload(sys)sys.setdefaultencoding('utf8')# by wangzclass Moofeel(object):    def __init__(self):        self.name = self.pwd = self.content = ''        self.cj = cookielib.LWPCookieJar()        try:            self.cj.revert('moofeel.cookie')        except Exception,e:            print e        self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj))        urllib2.install_opener(self.opener)    def setinfo(self,username,password):        '''設定使用者登入資訊'''        self.name = username        self.pwd = password    def login(self):        '''登入moofeel'''        params = {'username':self.name, 'password':self.pwd}        print 'login.......'        req = urllib2.Request(            'http://www.moofeel.com/member.php?mod=logging&action=login&loginsubmit=yes&infloat=yes',            urllib.urlencode(params)        )        self.operate = self.opener.open(req)        #print self.operate.geturl()        rawdata = self.operate.read()        rawdata = rawdata.decode(chardet.detect(rawdata)['encoding'])        if rawdata.find("登入失敗,你還可以嘗試") < 0 :            print 'Logged on successfully!'            self.cj.save('moofeel.cookie')            self.sign()        else:            print 'Logged on error'    def sign(self):        '''找到最新的領取MB的地址'''        siteurl="http://www.moofeel.com/forum-96-1.html"        isfind = False        count = 0        while(isfind == False):            count = count + 1            print '嘗試次數'+str(count)            req = urllib2.Request(siteurl)            self.operate = self.opener.open(req)            rawdata = self.operate.read()            rawdata = rawdata.decode(chardet.detect(rawdata)['encoding'])            for m in BeautifulSoup(rawdata).findAll(attrs={'class' : re.compile("new")}):                e = m.find(attrs={'class' : re.compile("xst")})            if(e!=None):                pattern = str(time.localtime()[0])+".*"+str(time.localtime()[1]) +".*"+ str(time.localtime()[2])                if(len(re.findall(pattern,str(e)))>0):                    realUrl = e.get('href')                    isfind = True                else:                    time.sleep(0.2)            else:                continue        print realUrl        self.replyAndFetch(realUrl)    def replyAndFetch(self,realUrl):        '''回複資訊並領取MB'''        req = urllib2.Request(realUrl)        self.operate = self.opener.open(req)        rawdata = self.operate.read()        #rawdata = rawdata.decode(chardet.detect(rawdata)['encoding'])        rawdata = rawdata.decode('gbk')        #擷取formhash        formhash = None        for m in BeautifulSoup(rawdata).findAll('input'):            if(m.get('name')=='formhash'):                formhash = m.get('value')        print formhash           #擷取form action        form_action = None             for m in BeautifulSoup(rawdata).findAll('form'):            if(m.get('id')=='fastpostform'):                form_action = m.get('action')        print form_action        #擷取領取MB地址        fetchmb_url = None        for m in BeautifulSoup(rawdata).findAll('a'):            for x in m.findAll('img'):                if(x.get('src').find('signin_reply')>0):                    fetchmb_url = m.get('href')        print fetchmb_url        #回複資訊        params = {'message':'I like this ~', 'formhash':formhash,'subject':''}        print 'reply.......'        req = urllib2.Request(            'http://www.moofeel.com/'+form_action,            urllib.urlencode(params)        )        self.operate = self.opener.open(req)        rawdata = self.operate.read()        #點擊領取MB        self.operate = urllib2.urlopen(fetchmb_url)        rawdata = self.operate.read()        print rawdata.decode('gbk')moofeel = Moofeel()username = ''#你的moofeel網的帳號password = ''#你的moofeel網的密碼moofeel.setinfo(username,password)moofeel.login()

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.