您现在的位置是:首页 > 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

本文标签:

很赞哦! ()

相关源码

  • (自适应响应式)高端简繁双语HTML5金融资本咨询单页pbootcms模板采用响应式设计确保在各类手机端设备很好的呈现。该模板专注于金融咨询、资本管理等领域企业形象展示,通过结构化布局突出行业专业度与可信度,后台数据同步管理简化内容维护流程。查看源码
  • (自适应)建站广告公司工作室作品展示品牌策划设计pbootcms源码下载基于PbootCMS开发的创意服务展示系统,内置作品集展示、服务流程图等专业模块,预设客户案例、设计流程等创意行业专属栏目结构。查看源码
  • (PC+WAP)盆栽绿植观赏植物花卉租赁免费pbootcms网站模板为花卉盆栽租赁、园林绿化企业打造的PbootCMS网站模板,采用较新内核开发,PC与移动端数据实时同步。通过清新自然的视觉设计,精准展现植物养护、花卉展示等业务场景,帮助用户快速建立专业级行业网站。查看源码
  • pbootcms模板(自适应手机版)红色响应式单位机构类网站自适应响应式单位机构网站模板 | PbootCMS内核开发为机构组织设计的响应式网站模板,采用PbootCMS内核开发,支持一键替换行业内容,满足多元化场景需求。查看源码
  • 自适应恒温恒湿机空调机械设备营销型网站模板(自适应手机版)响应式营销型恒温恒湿机环境设备类网站pbootcms模板 蓝色营销型空调设备网站源码下载PbootCMS内核开发的网站模板,该模板适用于营查看源码
  • html5响应式pbootcms模板新闻资讯博客网站源码该模板采用PbootCMS内核开发,专为新闻资讯类网站打造,同时具备高度行业适配性--只需替换图文内容即可快速转型为企业官网、行业门户等各类站点。查看源码
分享笔记 (共有 篇笔记)
验证码:

本栏推荐