首先要清楚discuz论坛发帖的流程,简单地说就是以下流程:
进入登录页 ->登录 -> 进入版面 ->发帖
登录和发帖时要获取到页面的formhash值,否则会失败,如果启用了验证码,还得去分析验证码,这就比较复杂了。这里只用python来描述这一系列过程,涉及到验证码还请大家自行去实现。
#!/usr/bin/env python
#coding=utf-8
from urllib import urlencode
import cookielib, urllib2,urllib
import os,sys
import urllib2,cookielib,urllib,httplib,re
import getpass
import time
from queue import queue
import threading
class discuz:
def __init__(self,uid,pwd,debug = false,**param):
self.username = uid
self.password = pwd
self.para = param
#self.timelimit = timelimit
self.regex = {
loginreg:,
postreg:
}
self.opener = none
self.request = none
self.islogin = false
self.donecount = 0
self.__login()
self.threadcount = 10
def __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 = true
except exception ,e:
print e
def 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 done
def __posttopic(self,**para):
if not self.islogin:
print please login......
return
while 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+=1
print %d done..... % self.donecount
except exception,e:
print e
time.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就是一个鸡肋 -_-