您现在的位置是:首页 > cms教程 > Discuz教程Discuz教程

python实现discuz论坛自动post登录发贴回帖

凝天2025-07-17Discuz教程已有人查阅

导读下面简单说下过程:首先是得到了login的post地址:几个关键的parameter是

#-*-coding:utf-8-*-
import urllib2, urllib, cookielib
import re
import getpass
import sqlite3
import random
import time
class Discuz:
def __init__(self,user,pwd,args):
self.username = user
self.password = pwd
self.args = args
self.regex = {
'loginreg':'',
'replyreg':'',
'tidreg': '[\s\S]+?'
}
self.conn = None
self.cur = None
self.islogin = False
self.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:', formhash
print '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 = True
print 'login success...'
except Exception,e:
print 'loggin error: %s' % e
def 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'] % tid
replysubmiturl = self.args['replysubmiturl'] % (fid,tid)
tidPage = urllib2.urlopen(tidurl).read()
formhash = re.search(self.regex['replyreg'], tidPage)
formhash = formhash.group(1)
#print 'reply formhash:', formhash
print '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]' % tidurl
else:
print 'Skip! Thread:%s is already replied...' % tid
except Exception, e:
print 'reply error: %s' % e
def GetTids(self, fid):
if self.islogin:
fidurl = self.args['fidurl'] % fid
response = urllib2.urlopen(fidurl)
content = response.read()
tids = re.findall(self.regex['tidreg'], content)
return tids
else:
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:// .xxx.com/logging.php?action=login',
'loginsubmiturl': 'http:// .xxx.com/logging.php?action=login&loginsubmit=yes',
'fidurl': 'http:// .xxx.com/forum-%s-1.html',
'tidurl': 'http:// .xxx.com/thread-%s-1-1.html',
'replysubmiturl': 'http:// .xxx.com/post.php?action=reply&replysubmit=yes&infloat=yes&handlekey=fastpost&fid=%s&tid=%s',
'referer':'http:// .xxx.com/index.php'
}
dz = Discuz(username, password,args)
fid = '45'
tids = dz.GetTids('45')
replylist = [
u'不错,支持一下,呵呵',
u'已阅,顶一下',
u'看看,顶你,呵呵',
u'多谢分享,顶一下',
u'说的不错,支持一下',
u'提着水桶到处转,哪里缺水哪里灌! ',
u'你太油菜了!'
]
for tid in tids:
content = random.choice(replylist)
content = content.encode('gbk')
dz.PostReply('45',tid, content)
time.sleep(20)
-
下面简单说下过程:
首先是得到了login的post地址:http:// .xxx.com/logging.php?action=login&loginsubmit=yes
几个关键的parameter是
formhash
cookietime
formhash
loginfield
password
questionid
referer
username

本文标签:

很赞哦! ()

相关源码

  • 帝国cms7.5奇闻异事末解之谜模板免费下载带数据本模板基于帝国CMS7.5系统开发,为神秘现象、未解之谜类主题网站设计。包含完整的PC端、移动端及百度MIP站同步生成功能,内置火车头采集规则模块,可快速采集目标站内容资源。整体设计风格神秘大气,符合主题定位。查看源码
  • 自适应黑色建筑装饰设计公司个人工作室pbootcms模板基于PbootCMS内核开发的黑金风格模板,为建筑装饰、工程设计类企业打造,采用‌开源架构‌,支持跨行业快速适配,核心优势如下: 查看源码
  • (自适应)双语网络电子摄像头设备pbootcms网站模板下载核心功能定位:基于PbootCMS开发的双语摄像头企业官网模板,深度适配产品规格书展示、实时监控演示、解决方案呈现等业务场景,支持中英文内容一键切换。查看源码
  • (PC+WAP)餐饮奶茶美食小吃招商加盟pbootcms模板源码下载为茶饮烘焙、小吃快餐等餐饮品牌打造的招商加盟系统,助力品牌快速拓展市场;双端pc+wap设计呈现加盟政策对比表。支持后台实时更新菜品图片、加盟费用等关键信息。查看源码
  • 帝国cms7.5品牌连锁店招商加盟商机网站模版源码本模板为招商加盟、创业投资、品牌连锁等商业领域设计,采用帝国CMS7.5内核构建,整体风格简洁大气,突出商业信任感与专业度,适合各类招商加盟项目展示、品牌连锁店宣传等商业应用场景。查看源码
  • (PC+WAP)绿色硅胶橡胶玩具制品营销型网站源码下载为硅胶橡胶制品及玩具行业打造的营销型网站模板,采用PbootCMS内核开发,通过模块化设计实现产品参数、安全认证、应用场景等专业内容的可视化呈现,助力企业建立可信赖的线上展示平台。查看源码
分享笔记 (共有 篇笔记)
验证码:

本栏推荐