2168575 发表于 2017-4-29 09:39:22

python 自动顶贴工具

#!D:\Program Files\Python25\python.exeimport urllib2, urllib, cookielibimport reimport getpassimport sqlite3import randomimport timeclass Discuz:def __init__(self,user,pwd,args):self.username = userself.password = pwdself.args = argsself.regex = {'loginreg':'<input\s*type="hidden"\s*name="formhash"\s*value="([\w\W]+?)"\s*\/>','replyreg':'<input\s*type="hidden"\s*name="formhash"\s*value="([\w\W]+?)"\s*\/>','tidreg': '<tbody\s*id="normalthread_\d+">[\s\S]+?<span\s*id="thread_(\d+)">'}self.conn = Noneself.cur = Noneself.islogin = Falseself.login()self.InitDB()def login(self):try:loginPage = urllib2.urlopen(self.args['loginurl']).read()formhash = re.search(self.regex['loginreg'], loginPage)formhash = formhash.group(1)#print 'login formhash:', formhashprint 'start login...'cj = cookielib.CookieJar()opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))user_agent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Mozilla/4.0 \(compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 2.0.507'opener.addheaders = [('User-agent', user_agent)]urllib2.install_opener(opener)logindata = urllib.urlencode({'cookietime':    2592000,'formhash': formhash,'loginfield':'username','username':    self.username,'password':    self.password,'questionid':    0,'referer': self.args['referer']})request = urllib2.Request(self.args['loginsubmiturl'],logindata)response = urllib2.urlopen(request)self.islogin = Trueprint 'login success...'except Exception,e:print 'loggin error: %s' % edef PostReply(self, fid, tid, content):try:sql = "select * from post where fid='%s' and tid='%s'" % (fid,tid)self.cur.execute(sql)if self.cur.rowcount == -1:tidurl = self.args['tidurl'] % tidreplysubmiturl = self.args['replysubmiturl'] % (fid,tid)tidPage = urllib2.urlopen(tidurl).read()formhash = re.search(self.regex['replyreg'], tidPage)formhash = formhash.group(1)#print 'reply formhash:', formhashprint 'start reply...'replydata = urllib.urlencode({'formhash': formhash,'message': content,'subject': '','usesig':'1'})request = urllib2.Request(replysubmiturl,replydata)response = urllib2.urlopen(request)sql = "insert into post values ('%s', '%s', '%d')" % (fid, tid, 1)self.cur.execute(sql)self.conn.commit()print 'reply success for [%s]' % tidurlelse:print 'Skip! Thread:%s is already replied...' % tidexcept Exception, e:print 'reply error: %s' % edef GetTids(self, fid):if self.islogin:fidurl = self.args['fidurl'] % fidresponse = urllib2.urlopen(fidurl)content = response.read()tids = re.findall(self.regex['tidreg'], content)return tidselse:print 'Error Please Login...'def InitDB(self):self.conn = sqlite3.connect('data.db')self.cur = self.conn.cursor()sql = '''create table if not exists post (fid text,tid text,replied integer)'''self.cur.execute(sql)self.conn.commit()if __name__ == '__main__':username = raw_input('username:').strip()password = getpass.getpass('password:').strip()args = {'loginurl': 'http://bbs.tigtag.com/logging.php?action=login','loginsubmiturl': 'http://bbs.tigtag.com/logging.php?action=login&loginsubmit=yes','fidurl': 'http://bbs.tigtag.com/forum-%s-1.html','tidurl': 'http://bbs.tigtag.com/thread-%s-1-1.html','replysubmiturl': 'http://bbs.tigtag.com/post.php?action=reply&replysubmit=yes&infloat=yes&handlekey=fastpost&fid=%s&tid=%s','referer':'http://bbs.tigtag.com/index.php'}dz = Discuz(username, password,args)fid = '48'tids = dz.GetTids('48')replylist = while True:tid = 1412440content = random.choice(replylist)content = content.encode('gbk')dz.PostReply('48',tid, content)time.sleep(20)
页: [1]
查看完整版本: python 自动顶贴工具