用Splinter实现自动发帖
import random import splinter import time import re #changePage用来生产不同页面的链接 def changePage(url,pageNumber): now_page = int(re.search('thread-(\d+)-1-1',url,re.S).group(1)) page_group = [] for i in range(now_page,now_page+pageNumber): link = re.sub('thread-(\d+)-1-1','thread-%s-1-1'%i,url,re.S) page_group.append(link) return page_group #随机生成回帖内容 def replygGenerator(): replylist=[u'学习了,支持一下',u'学习了,谢谢分享',u'学习了,正好用到',u'学习了,最近正在研究'] n = random.randint(0,3) return replylist[n] if __name__ == '__main__': mybrowser = splinter.Browser('chrome') url = "http://bbs.xxxxx.com/thread-100101-1-1.html" mybrowser.visit(url) mybrowser.find_by_id('ls_username').fill('xxxxxx') mybrowser.find_by_id('ls_password').fill('xxxxxx') time.sleep(10) #等待用户确定登录 linksNum = 10 #发帖数量 allLinks = changePage(url,linksNum) for link in allLinks: mybrowser.visit(link) if mybrowser.is_text_present(u'抱歉'):continue #判断被删除的帖子,"抱歉,指定的主题不存在或已被删除或正在被审核" mypost = replygGenerator() mybrowser.find_by_name('message').fill(mypost) mybrowser.find_by_name('replysubmit').click() print '灌水完毕,well done!' # mybrowser.quit()