python实现discuz论坛批量发帖的方式,爱在灵灵久博客认为主要分为两类,一是通过登录discuz论坛进行发帖(这种方式也可以登录第三方的网站来批量发帖);
二是作为站长直接通过写入数据库来发帖,可以实现无限量发帖,发帖速度快。
第一种方式实现的已经有很多人介绍了,
这里主要介绍第二种方式直接用python3写入discuz论坛数据库批量发帖可带图文。
一、discuz数据库发帖原理在介绍数据发帖前先来了解一下discuz论坛发帖涉及到的数据库: 1、主题表 pre_forum_thread:这个表一个主要数据就是 tid 主题ID
2、post 分表协调表 pre_forum_post_tableid:这里需要获取一个自增的 pid
3、帖子表 pre_forum_post :记录主题pid、fid、tid、title、content等主要信息
4、版块表 pre_forum_forum:这里主要更新版块的主题、帖子数量
5、帖子主题审核数据表 pre_forum_thread_moderate:这个可以根据自己状况决定,并不是必须的(可以省略)
6、用户统计表 pre_common_member_count:主要是更新用户的主题数量 pre_common_member_count表和pre_forum_forum表两个表中主要修改帖子数据量其中主要涉及到以下几个字段: threads: 版块内的主题数. posts: 版块内的帖子数.(主题数和帖子数是有区别的,发布的一个帖子会同时增加主题数和帖子数,而回复一个帖子只会增加一个帖子数不会增加主题数) todayposts: 版块内, 今日发帖的个数. 这个是post的个数, 不是thread的个数. lastpost: 这个字段比较奇葩, 看名字它是表示本版块最新一个帖子. 但它的值比较有意思, 这是一个字符串, 由四部分组成, 每部分之间用 \t 制表符分割. 第一部分是这个帖子的pid, 第二部分是帖子的标题, 第三部分是帖子的发帖时间, 第四部分是帖子的作者名. 这个字段可能是为了提高论坛首页的性能, 有了他之后,首页就负担轻了很多。 二、python数据库发帖环境本次测试使用的是Windows10 64位的操作系统 python3.6的版本,pycharm的编辑器,本地搭建的discuz论坛网站(也可直接使用上线的discuz论坛网站,不过建议先在本地进行测试),另外需要安装pymysql库,通过pip install pymysql安装上即可。 三、python写入数据库的步骤discuz 发帖流程主要分为5个步骤: 第一步:给pre_forum_post_tableid表插入空数据进行自增,然后获取自增pid。 - cursor.execute('INSERT INTO pre_forum_post_tableid VALUES (NULL);')
- cursor.execute('SELECT max(pid) FROM pre_forum_post_tableid;')
- pid = cursor.fetchone()[0]
複製代碼第二步:向 主题表 pre_forum_thread 中插入版块ID、用户ID、用户名、帖子标题、发帖时间等信息,并获取主题的ID作为tid。 - sql_thread="INSERT INTO pre_forum_thread SET fid="+str(fid)+",author='"+author+"',authorid="+str(uid)+",subject='"+subject+"',dateline="+str(int(time.time()))+",lastposter='"+author+"',lastpost="+str(int(time.time()))+",views="+str(view)+";"
- cursor.execute(sql_thread)
- cursor.execute('SELECT max(tid) FROM pre_forum_thread')
- tid = int(cursor.fetchone()[0])
複製代碼第三步:向帖子表 pre_forum_post 中插入帖子相关信息,这里需要注意的是: pid为第一步的pid值,tid为第二步的tid值 - sql_post = "INSERT INTO pre_forum_post SET pid="+str(pid)+",fid="+str(fid)+",tid="+str(tid)+",first=1,author='"+author+"', authorid="+str(uid)+", subject='"+subject+"' ,dateline="+str(int(time.time()))+", message='" + message + "' , useip='140.112.218.141' , port=11560 , invisible = 0, anonymous = 0 , usesig = 1 , htmlon = 1 , bbcodeoff =-1 , smileyoff =-1 , parseurloff =0 , attachment = 0 , tags='' , replycredit=0 , status=0;"
複製代碼第四步:更新版块 pre_forum_forum 相关主题、帖子数量信息 - sql_forum = 'UPDATE pre_forum_forum SET threads=threads+1, posts=posts+1, todayposts=todayposts+1 , allowsmilies = 1,allowbbcode = 1, allowimgcode =1 ,allowspecialonly = 1,allowglobalstick = 1,alloweditpost = 1 ,recyclebin =1 WHERE fid='+str(fid)+';'
複製代碼第五步:更新用户 pre_common_member_count 帖子数量信息 - sql_count = 'UPDATE pre_common_member_count SET threads = threads+1 WHERE uid ='+str(uid)+';'
複製代碼discuz发帖过程主要就是以上5个步骤,通过这几个步骤就可以实现discuz的发帖流程,其中涉及到一些积分等其他信息的可以自己加上。另外,通过数据库发帖还可以发布带图片的帖子,只需先将图片直接上传到网站中图片附件对应存放的位置,然后将其形成链接(直接上传的图片文件名称最好是用拼音或数字,不要带中文),在发帖时内容里面加入img标签进行解析图片地址即可形成带图文的帖子。或者,直接采集其他内容源码后作为帖子内容,同时安装一个图片本地化插件即可实现带图片的帖子。 以上方法可以实现discuz论坛批量发布图文帖子,可以解决大部分站长的需求,但是却不能发布带附件的帖子,因此需要想其他办法。 - def post_data(conn):
- cursor = conn.cursor()
- try:
- cursor.execute('SELECT username FROM pre_common_member WHERE uid = '+str(uid)+";")
- author = cursor.fetchone()[0] # 用户name
- print(author)
- # 第一步给pre_forum_post_tableid表插入空数据进行自增,然后获取自增pid
- cursor.execute('INSERT INTO pre_forum_post_tableid VALUES (NULL);')
- cursor.execute('SELECT max(pid) FROM pre_forum_post_tableid;')
- pid = cursor.fetchone()[0]
- # print(pid)
- # 第二步给pre_forum_thread表插入帖子标题数据,然后获取自增tid
- sql_thread = "INSERT INTO pre_forum_thread SET fid="+str(fid)+",author='"+author+"',authorid="+str(uid)+",subject='"+subject+"',dateline="+str(int(time.time()))+",lastposter='"+author+"',lastpost="+str(int(time.time()))+",views="+str(view)+";"
- cursor.execute(sql_thread)
- cursor.execute('SELECT max(tid) FROM pre_forum_thread')
- tid = int(cursor.fetchone()[0])
- # print(tid)
- # 第三步给pre_forum_post表插入帖子的标题、内容等,pid、tid用上两步获得的数据 如要增加附件需修改attachment
- sql_post = "INSERT INTO pre_forum_post SET pid="+str(pid)+",fid="+str(fid)+",tid="+str(tid)+",first=1,author='"+author+"', authorid="+str(uid)+", subject='"+subject+"' ,dateline="+str(int(time.time()))+", message='" + message + "' , useip='140.112.218.141' , port=11560 , invisible = 0, anonymous = 0 , usesig = 1 , htmlon = 1 , bbcodeoff =-1 , smileyoff =-1 , parseurloff =0 , attachment = 0 , tags='' , replycredit=0 , status=0;"
- # print(sql_post)
- cursor.execute(sql_post)
- # cursor.execute('SELECT max(aid) FROM pre_forum_attachment')
- # aid = int(cursor.fetchone()[0]) + 1
- # 第四步给pre_forum_forum版块表进行更新帖子数量
- sql_forum = 'UPDATE pre_forum_forum SET threads=threads+1, posts=posts+1, todayposts=todayposts+1 , allowsmilies = 1,allowbbcode = 1, allowimgcode =1 ,allowspecialonly = 1,allowglobalstick = 1,alloweditpost = 1 ,recyclebin =1 WHERE fid='+str(fid)+';'
- # print(sql_forum)
- cursor.execute(sql_forum)
- # 第五步给pre_common_member_count表更新用户帖子数量信息
- sql_count = 'UPDATE pre_common_member_count SET threads = threads+1 WHERE uid ='+str(uid)+';'
- cursor.execute(sql_count)
- # cursor.execute('INSERT INTO pre_forum_attachment_" + str(aid % 10) + " SET `readperm`='0' , `price`='10' , `tid`='" + str(tid) + "' , pid=' + pid+ ',uid=1 , description=, aid=' + str(aid) + ' ,dateline='+ str(int(time.time())) + ',filename="' + att_name + '", filesize=4, attachment="upload/' + att_name + '",remote=0, isimage=0, width=0, thumb=0;')
- # cursor.execute('INSERT INTO pre_forum_attachment SET tid=' + str(tid) + ', pid=' +str(pid)+ ', tableid='+ str(aid % 10) + ', aid=' + str(aid) + ';')
- # 提交,不然无法保存新建或者修改的数据
- conn.commit()
- except:
- print("写入数据库失败,事物回滚!")
- conn.rollback()
- finally:
- cursor.close()
複製代碼
|