Discuz 论坛评论回复代码 发表于 2020-09-21 | 字数统计: 1.6k字 | 阅读时长 ≈ 9分钟 本来想着刷下积分,换点礼品,结果居然好有限制。附上代码。更新代码,增加摇一摇赚取浪花的功能。找了台服务器,摇浪花啦。 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207#-\*-coding:utf-8-\*-import urllib2, urllib, cookielibimport reimport getpassimport sqlite3import urlparseimport randomimport timeimport PyV8from bs4 import BeautifulSoupclass 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\*\\/>', #input type="hidden" name="formhash" value="7f420ac4" '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+)">', 'subform':'<a href="forum.php\\?mod=forumdisplay&fid=\\d+">\\S+</a>', 'topidreg':'<a href="forum.php\\?mod=viewthread&tid=\\d+&extra=page%3D1" onclick="atarget\\(this\\)" class="s xst">\\S+</a>' } self.subform=\['OpenStack',u'云服务',u'云管理','hadoop','storm','spark'\] self.conn = None self.cur = None self.islogin = False self.login() self.InitDB() def login(self): try: loginPage = urllib2.urlopen(self.args\['loginurl'\]).read() 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, 'quickforward': 'yes', 'fastloginfield':'username', 'handlekey':'ls', '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, topicUrl,content): try: if self.islogin: temp=self.args\['replysubmiturl'\] replysubmiturl = temp %(fid,tid) response = urllib2.urlopen(topicUrl) content11 = response.read() # print content11 # update url url=self.getNewUrlFromJs(content11); newTopicUrl= self.args\['redirectBaseUrl'\]+url response = urllib2.urlopen(newTopicUrl) content11 = response.read() formhashs = re.search(self.regex\['replyreg'\], content11) formhash= formhashs.group(1) print formhash replydata = urllib.urlencode({ 'formhash': formhash, 'message': content, 'subject': '', 'usesig':'1' }) request = urllib2.Request(replysubmiturl,replydata) response = urllib2.urlopen(request) print 'reply success for \[%s\]' % topicUrl else: print 'user not login' except Exception, e: print 'reply error: %s' % e def GetSubForms(self): if self.islogin: fidurl = self.args\['fidurl'\] response = urllib2.urlopen(fidurl) content = response.read() soup = BeautifulSoup(content,"html.parser") h1userSoupList=soup.findAll(name="a", attrs={"href":re.compile(r"forum.php\\?mod=forumdisplay&fid=\\d+")}) subformDicts={} for subform in h1userSoupList: if subform.get\_text() in self.subform: # subforms.append(self.args\['baseUrl'\]+subform\["href"\]) url=self.args\['baseUrl'\]+subform\["href"\] result=urlparse.urlparse(url) params=urlparse.parse\_qs(result.query,True) tag= params\['fid'\]\[0\] subformDicts\[tag\]=url return subformDicts else: print 'Error Please Login...' def getNewUrlFromJs(self,js): js=js\[31:-9\] for st in \['window','location',"'assign'","'href'","'replace'"\]: equal=re.findall('\[\_A-Za-z0-9 =\]+%s;'%st,js) if equal==\[\]: continue else: equal=equal\[0\] var=equal.split('=')\[0\].strip() js=js.replace(equal,'') js=js.replace(var,st) js=js.replace("\['%s'\]"%st.strip("'"),'.%s'%st.strip("'")) if re.findall('window\\.href=.+',js)!=\[\]: js=js.replace(re.findall('window\\.href=.+',js)\[0\],'') js=js.replace('location.href=','').replace('location.replace','').replace('location.assign','') ctxt2 = PyV8.JSContext() ctxt2.enter() return ctxt2.eval(js) def haveAGoodLuck(self): luckurl='http://bbs.iop365.com/iop//plugin.php?id=yinxingfei\_zzza:yinxingfei\_zzza\_hall&yjjs=yes' response = urllib2.urlopen(luckurl) content = response.read() formhashs = re.search(self.regex\['replyreg'\], content) formhash= formhashs.group(1) soup = BeautifulSoup(content,"html.parser") submitUrl2='http://bbs.iop365.com/iop//plugin.php?id=yinxingfei\_zzza:yinxingfei\_zzza\_post' replydata = urllib.urlencode({ 'formhash': formhash }) request = urllib2.Request(submitUrl2,replydata) response = urllib2.urlopen(request) def visitUser(self): baseUrl='http://bbs.iop365.com/iop/?%s' for i in range(1,10): randNum=random.randint(1, 200) visitUrl=baseUrl %(randNum) response = urllib2.urlopen(visitUrl) content = response.read() print visitUrl 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() def getTopicList(self,sunFormUrl): topicDis={} if self.islogin: fidurl = sunFormUrl response = urllib2.urlopen(fidurl) content = response.read() soup = BeautifulSoup(content,"html.parser") h1userSoupList=soup.findAll(name="a", attrs={"href": re.compile(r"forum.php\\?mod=viewthread\\&"),"onclick":"atarget(this)"}) for topic in h1userSoupList: #topics.append(self.args\['baseUrl'\]+topic\["href"\]) url=self.args\['baseUrl'\]+topic\["href"\] result=urlparse.urlparse(url) params=urlparse.parse\_qs(result.query,True) topicDis\[params\['tid'\]\[0\]\]=url return topicDis else: print 'user doest not login'if \_\_name\_\_ == '\_\_main\_\_': #username = raw\_input('username:').strip() #password = getpass.getpass('password:').strip() args = { 'loginurl': 'http://bbs.iop365.com/forum.php', 'loginsubmiturl': 'http://bbs.iop365.com/iop/member.php?mod=logging&action=login&loginsubmit=yes&infloat=yes&lssubmit=yes&inajax=1', 'fidurl': 'http://bbs.iop365.com/iop/forum.php', 'tidurl': 'http://bbs.iop365.com/thread-%s-1-1.html', 'replysubmiturl': 'http://bbs.iop365.com/iop//forum.php?mod=post&action=reply&fid=%s&tid=%s&extra=&replysubmit=yes&infloat=yes&handlekey=fastpost&inajax=1', 'referer':'http://bbs.iop365.com/forum.php' , 'baseUrl':'http://bbs.iop365.com/iop/', 'redirectBaseUrl': 'http://bbs.iop365.com/'} userInfo={'wangdk':'\*\*\*\*'} for (username,password) in userInfo.items(): dz = Discuz(username, password,args) subforms = dz.GetSubForms() dz.visitUser() topicNum=0 allTopics={} for fid in subforms: topics=dz.getTopicList(subforms\[fid\]) for tid in topics: topicNum=topicNum+1 allTopics\[topicNum\]={'fid':fid,'tid':tid,'topicUrl':topics\[tid\]} randNum=random.randint(1, topicNum) print 'good luck time=%s',time.strftime( "%Y-%m-%d %H:%M:%S", time.localtime() ) dz.PostReply(allTopics\[randNum\]\['fid'\],allTopics\[randNum\]\['tid'\],allTopics\[randNum\]\['topicUrl'\],'guanshui,haha') print 'topicUrl %s',allTopics\[randNum\]\['topicUrl'\] dz.haveAGoodLuck()