您现在的位置是:首页 > 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
本文标签:
很赞哦! ()
上一篇:PHP的discuz模板语法介绍
下一篇:discuz NT整合
相关教程
图文教程
discuz的C::t方法实例
一、C::t方法的好处:一是对象清楚,二是对形参格式化处理,三是可集中SQL语句,利于维护,四是安全性更高。二、具体用法,看下面的例子
discuz是什么,discuz有什么用
discuz是一种功能强大、灵活性高、安全稳定的开源论坛软件,是一个基于PHP和MySQL的在线社区平台,提供了一个完整的论坛系统,包括帖子、主题、用户管理、权限控制等功能。
discuz全局数组变量后台各项设置教程
$_G 保存了Discuz中所有的预处理数据缓存能够很好的提高程序的性能,一些配置数据没必要每次都查询数据库,只要在修改了的时候更新下缓存即可。Discuz! 中所有的缓存保存在 $_G[cache] 中
Discuz解决安装时报错run_sql_error的方法
本文实现功能解决在MySQL8.0安装discuz报错run_sql_error如果想直接看解决结果,直接去看解决问题目录
相关源码
-
(自适应)幽默笑话脑筋急转弯搞笑趣图博客pbootcms模板下载本模板基于PbootCMS系统开发,专为幽默笑话、搞笑趣图类网站设计。采用轻松活泼的布局风格,突出娱乐内容分享特色,适合各类笑话、段子、搞笑图片等内容展示。查看源码 -
(自适应)家政保洁保姆打扫卫生清灰服务pbootcms模板免费下载采用手工编写的DIV+CSS架构,代码结构清晰无冗余,加载速度优异。响应式设计适配各类终端设备,保障手机、平板、电脑端的一致浏览体验。查看源码 -
自适应手机版五金机械阀门设备通用行业网站模板该PbootCMS内核开发的网站模板适用于阀门设备、五金机械类企业,通过更换文字图片也可快速适配其他工业领域。采用响应式设计,确保PC端与手机端数据同步,操作便捷,并附带测试数据。查看源码 -
(自适应)科技产品设备技术作品pbootcms网站模板带下载和招聘基于PbootCMS内核开发的高端科技企业模板,采用响应式布局技术,适配各类移动终端设备。模板设计聚焦科技行业特性,通过模块化结构实现企业形象展示、技术成果发布与人才招募等核心需求查看源码 -
(PC+WAP)蓝色低碳环保隔断板装修装饰类网站pbootcms源码下载本款基于PbootCMS开发的网站模板为活动隔断板、装修装饰行业打造,特别适合移动隔断、环保隔断、办公分区等产品的展示与推广。查看源码 -
(自适应)蓝色英文外贸电子科技产品带三级栏目网站模板为外贸企业设计的英文网站模板,基于PbootCMS系统开发。突出多语言支持和国际化布局,三级栏目结构清晰展示产品分类,响应式设计确保更好客户在移动端和PC端获得一致的专业体验。查看源码
| 分享笔记 (共有 篇笔记) |
