首先要清楚discuz论坛发帖的流程,简单地说就是以下流程:
进入登录页 ->登录 -> 进入版面 ->发帖
登录和发帖时要获取到页面的formhash值,否则会失败,如果启用了验证码,还得去分析验证码,这就比较复杂了。这里只用python来描述这一系列过程,涉及到验证码还请大家自行去实现。
#!/usr/bin/env python#coding=utf-8from urllib import urlencodeimport cookielib, urllib2,urllibimport os,sysimport urllib2,cookielib,urllib,httplib,reimport getpassimport timefrom queue import queueimport threadingclass discuz:def __init__(self,uid,pwd,debug = false,**param):self.username = uidself.password = pwdself.para = param#self.timelimit = timelimitself.regex = {loginreg:,postreg:}self.opener = noneself.request = noneself.islogin = falseself.donecount = 0self.__login()self.threadcount = 10def __login(self):try:loginpage = urllib2.urlopen(self.para[loginurl]).read()formhash = re.search(self.regex[loginreg],loginpage)formhash = formhash.group(1)print start login......cookiejar = cookielib.cookiejar()self.opener = urllib2.build_opener(urllib2.httpcookieprocessor(cookiejar))values = {formhash:formhash,username:self.username,password:self.password,loginsubmit:true}data = urllib.urlencode(values)self.request = urllib2.request(self.para[loginsubmiturl], data)rq = self.opener.open(self.request)print login success......self.islogin = trueexcept exception ,e:print edef post(self,subject,wysiwyg,content):threads = []for i in range(self.threadcount):t = threading.thread(target=self.__posttopic,kwargs={_subject:subject,_wysiwyg:wysiwyg,_body:content})threads.append(t)for i in range(self.threadcount):threads[i].start()for i in range(self.threadcount):threads[i].join()print donedef __posttopic(self,**para):if not self.islogin:print please login......returnwhile true:try:self.request = urllib2.request(self.para[posturl])rq = self.opener.open(self.request)data = rq.read()formhash = re.search(self.regex[postreg],data)formhash = formhash.group(1)postdata = {addtags:+可用标签,checkbox:0,formhash:formhash,iconid:,message:para[_body],subject:para[_subject],tags:,updateswfattach : 0,wysiwyg : para[_wysiwyg]}self.request = urllib2.request(self.para[postsubmiturl],urllib.urlencode(postdata))self.opener.open(self.request)self.donecount+=1print %d done..... % self.donecountexcept exception,e:print etime.sleep(2)if __name__==__main__:name = raw_input(username:)password = getpass.getpass(password:)dz = discuz(name,password,loginurl=http://xxx/logging.php?action=login,loginsubmiturl=http://xxx/logging.php?action=login&loginsubmit=yes,posturl=http://xxx/post.php?action=newthread&fid=5,postsubmiturl=http://xxx/post.php?&action=newthread&fid=5&extra=&topicsubmit=yes)content=这是帖子内容dz.post(这是帖子内容,1,content)
discuz类中只有一个公开方法
def post(self,subject,wysiwyg,content)
三个参数,分别是帖子标题、是否是可视化编辑、帖子内容。
类实例化时会进行登录操作,并同时记录下cookie以备用,post方法中会启用多个线程执行私有方法__posttopic(self,**para)进行真正的发帖操作。
所以,基本上discuz生成的formhash就是一个鸡肋 -_-
新闻热点
疑难解答