您现在的位置是:首页 > cms教程 > Discuz教程Discuz教程
Python实现Discuz论坛的自动POST登录发贴回帖实例
代真2025-07-10Discuz教程已有人查阅
导读下面简单说下过程:首先是得到了login的post地址:几个关键的parameter是loginurl为登录面页,用于获得formhash的值loginsubmiturl为post登录参数的地址
#-*-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':'<input\s*type="hidden"\s*name="formhash"\s*value="([\w\W]+?)"\s*\/>',
'replyreg':'<input\s*type="hidden"\s*name="formhash"\s*value="([\w\W]+?)"\s*\/>',
'tidreg': '<tbody\s*id="normalthread_\d+">[\s\S]+?<span\s*id="thread_(\d+)">'
}
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地址:
几个关键的parameter是
formhash
cookietime
formhash
loginfield
password
questionid
referer
username
cookietime 浏览器自动给的是 2592000
loginfield 默认的username
password 密码
questionid 这个貌似是登录时的回答问题,这个论坛没有强制回答所以用默认的0
referer 这个则是引用地址
username 用户名
formhash 之后这个貌似这个是随机的,不固定,可也是个关键参数,所以就直接用正则查找之
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'
}
loginurl为登录面页,用于获得formhash的值loginsubmiturl为post登录参数的地址
fidurl这个是版块的ID,url中%s那里即为fid,这样的url ,fid即为45
tidurl是帖子的id,查找方法同上
replysubmiturl这个是回复帖子post参数的url,要定位一个帖子前提得知道fid和tid
referer这个是引用地址,用网站的首页即可
本文标签:
很赞哦! ()
上一篇:discuz脚本发表帖子的方法
下一篇:怎么修改discuz首页logo
相关教程
图文教程
discuz!nt投票功能写法实例
可能要写一个投票系统,把discuz!net里面的代码放到这里来参考参考,感觉它的投票功能做的有点简单,数据库里面相关的就两个主从表,不过想想也可以理解
discuz怎么去除.php后缀
discuz去除“.php”的方法:首先点击“后台-界面-导航设置-主导航”;然后点击“添加主导航”新建一个导航栏目;接着把默认论坛页面“forum.ph
Discuz的authcode函数注释
Discuz函数中最经典的函数是authcode函数,因为supesite,UCenterHome,UCenter,Discuz X都使用了这个函数进行加密
Discuz论坛自动发帖机实现方法
1. 起因:某人在自己的空间搭了个discuz,设置了诸多必须灌水的邪恶规定。本人一向潜水员当惯了当然不爽。a. discuz论坛无任何图片验证码,包括登陆以及发帖的时候。
相关源码
-
(自适应响应式)双语LED照明灯饰灯具外贸网站pbootcms源码下载模板采用响应式设计,能自动适应手机、平板和电脑等多种设备屏幕,确保用户在不同设备上都能获得良好的浏览体验。同一后台管理,数据实时同步,操作简便高效。查看源码 -
(PC+WAP)激光水幕音乐喷泉设备工程网站源码下载本模板基于PbootCMS系统开发,为喷泉设备工程类企业设计,特别适合展示音乐喷泉、激光水幕等水景艺术项目。采用响应式技术,确保各类工程案例在不同设备上都能呈现视觉效果。查看源码 -
粉色家政月嫂保姆公司pbootcms网站模板(PC+WAP)为家政服务、月嫂保姆企业打造的营销型解决方案,基于PbootCMS内核开发,采用温馨粉色主题传递行业温度。PHP7.0+高性能架构支持SQLite/MySQL双数据库查看源码 -
pbootcms网站模板响应式全屏旅游景区网站源码本模板为风景民宿、旅游景区等企业设计,基于PbootCMS内核开发,具备响应式布局与专业SEO优化功能,助力企业低成本高效获客。以下是核心特点:查看源码 -
自适应极简风个人博客文章自媒体网站模板基于PbootCMS开源内核开发的极简个人博客模板,采用移动优先设计原则,通过Media Query技术实现320px至1920px六级分辨率适配,确保在手机、平板及PC设备上均呈现良好的视觉体验。查看源码 -
(自适应)家政保洁保姆打扫卫生清灰服务pbootcms模板免费下载采用手工编写的DIV+CSS架构,代码结构清晰无冗余,加载速度优异。响应式设计适配各类终端设备,保障手机、平板、电脑端的一致浏览体验。查看源码
| 分享笔记 (共有 篇笔记) |
