此代码仅作学习交流。不足之处请多多指教。如有转载请保留出处。
专用于论坛回帖(代码中以威锋网为例),因为当时觉得发帖刷等级太打眼了,所以设定为回贴来获取经验。有需要的朋友改下回复参数即可实现发帖功能。
程序经过检验是可行的,从小苹果一路升级到金苹果。但是后来被发现了,就扣分又给我扣成小苹果了。检查原因是因为回帖的帖子是使用随机函数随机生成的,所以存在一定的缺陷性。后来想到一个好的机制,但是也对等级没的那么狂热了,一直也没有实现。这里抛钻引玉一下。
先取得昨天发帖且回复大于10的帖子地址,然后在已经已有的回复中随机选择一条来作为自己的回复。这样一来避免挖坟被发现,二来也规避了千篇一律的回帖方式。降低被发现的可能。
贴上代码,大家看看即可。
程序使用了一个“config.txt”的回复列表。这是万能回复,针对不同类型的帖子给予不同的回复,可自行编辑。附上:
{'AppStore游戏':{'ht0':'感谢分享,,,','ht1':'威锋有你更精彩','ht2':'谢谢了','ht12':'看起来不错啊','ht3':'谢谢楼主了','ht3':'楼主好淫啊,收下了','ht4':'试试看…………','ht5':'先顶了再说','ht6':'感谢分享^_^','ht7':'留名,一会看','ht8':'帮顶、求分!','ht9':'助人为乐^_^','ht10':'精品文章^_^','ht11':'淡淡的路过'}} {'Cydia软件':{'ht0':'感谢分享,,,','ht1':'威锋有你更精彩','ht2':' 谢谢了 [s:10]','ht12':'看起来不错啊','ht3':'谢谢楼主了','ht3':'楼主好淫啊,收下了','ht4':'试试看…………','ht5':'先顶了再说','ht6':'感谢分享^_^','ht7':'留名,一会看','ht8':'帮顶、求分!','ht9':'助人为乐^_^','ht10':'精品文章^_^','ht11':'淡淡的路过'}} {'Wefiler|威锋盘分享':{'ht0':'感谢分享,,,','ht1':'威锋有你更精彩','ht2':' 谢谢了 [s:11]','ht12':'看起来不错啊','ht3':'谢谢楼主了','ht3':'楼主好淫啊,收下了','ht4':'试试看…………','ht5':'先顶了再说','ht6':'感谢分享^_^','ht7':'留名,一会看','ht8':'帮顶、求分!','ht9':'助人为乐^_^','ht10':'精品文章^_^','ht11':'淡淡的路过'}} {'畅销图书':{'ht0':'感谢分享,,,','ht1':'威锋有你更精彩','ht2':' 谢谢了 [s:12]','ht12':'看起来不错啊','ht3':'谢谢楼主了','ht3':'楼主好淫啊,收下了','ht4':'试试看…………','ht5':'先顶了再说','ht6':'感谢分享^_^','ht7':'留名,一会看','ht8':'帮顶、求分!','ht9':'助人为乐^_^','ht10':'精品文章^_^','ht11':'淡淡的路过'}} {'电子书':{'ht0':'感谢分享,,,','ht1':'威锋有你更精彩','ht2':' 谢谢了 [s:13]','ht12':'看起来不错啊','ht3':'谢谢楼主了','ht3':'楼主好淫啊,收下了','ht4':'试试看…………','ht5':'先顶了再说','ht6':'感谢分享^_^','ht7':'留名,一会看','ht8':'帮顶、求分!','ht9':'助人为乐^_^','ht10':'精品文章^_^','ht11':'淡淡的路过'}} {'汉化':{'ht0':'感谢分享,,,','ht1':'威锋有你更精彩','ht2':' 谢谢了 [s:15]','ht12':'看起来不错啊','ht3':'谢谢楼主了','ht3':'楼主好淫啊,收下了','ht4':'试试看…………','ht5':'先顶了再说','ht6':'感谢分享^_^','ht7':'留名,一会看','ht8':'帮顶、求分!','ht9':'助人为乐^_^','ht10':'精品文章^_^','ht11':'淡淡的路过'}} {'汉化专用':{'ht0':'感谢分享,,,','ht1':'威锋有你更精彩','ht2':' 谢谢了 [s:16]','ht12':'看起来不错啊','ht3':'谢谢楼主了','ht3':'楼主好淫啊,收下了','ht4':'试试看…………','ht5':'先顶了再说','ht6':'感谢分享^_^','ht7':'留名,一会看','ht8':'帮顶、求分!','ht9':'助人为乐^_^','ht10':'精品文章^_^','ht11':'淡淡的路过'}} {'铃声':{'ht0':'感谢分享,,,','ht1':'威锋有你更精彩','ht2':' 谢谢了 [s:17]','ht12':'看起来不错啊','ht3':'谢谢楼主了','ht3':'楼主好淫啊,收下了','ht4':'试试看…………','ht5':'先顶了再说','ht6':'感谢分享^_^','ht7':'留名,一会看','ht8':'帮顶、求分!','ht9':'助人为乐^_^','ht10':'精品文章^_^','ht11':'淡淡的路过'}} {'漫画':{'ht0':'感谢分享,,,','ht1':'威锋有你更精彩','ht2':' 谢谢了 [s:18]','ht12':'看起来不错啊','ht3':'谢谢楼主了','ht3':'楼主好淫啊,收下了','ht4':'试试看…………','ht5':'先顶了再说','ht6':'感谢分享^_^','ht7':'留名,一会看','ht8':'帮顶、求分!','ht9':'助人为乐^_^','ht10':'精品文章^_^','ht11':'淡淡的路过'}} {'美化':{'ht0':'感谢分享,,,','ht1':'威锋有你更精彩','ht2':' 谢谢了 [s:19]','ht12':'看起来不错啊','ht3':'谢谢楼主了','ht3':'楼主好淫啊,收下了','ht4':'试试看…………','ht5':'先顶了再说','ht6':'感谢分享^_^','ht7':'留名,一会看','ht8':'帮顶、求分!','ht9':'助人为乐^_^','ht10':'精品文章^_^','ht11':'淡淡的路过'}} {'其他书籍':{'ht0':'感谢分享,,,','ht1':'威锋有你更精彩','ht2':' 谢谢了 [s:20]','ht12':'看起来不错啊','ht3':'谢谢楼主了','ht3':'楼主好淫啊,收下了','ht4':'试试看…………','ht5':'先顶了再说','ht6':'感谢分享^_^','ht7':'留名,一会看','ht8':'帮顶、求分!','ht9':'助人为乐^_^','ht10':'精品文章^_^','ht11':'淡淡的路过'}} {'软件':{'ht0':'感谢分享,,,','ht1':'威锋有你更精彩','ht2':' 谢谢了 [s:21]','ht12':'看起来不错啊','ht3':'谢谢楼主了','ht3':'楼主好淫啊,收下了','ht4':'试试看…………','ht5':'先顶了再说','ht6':'感谢分享^_^','ht7':'留名,一会看','ht8':'帮顶、求分!','ht9':'助人为乐^_^','ht10':'精品文章^_^','ht11':'淡淡的路过'}} {'软件分享':{'ht0':'感谢分享,,,','ht1':'威锋有你更精彩','ht2':' 谢谢了 [s:22]','ht12':'看起来不错啊','ht3':'谢谢楼主了','ht3':'楼主好淫啊,收下了','ht4':'试试看…………','ht5':'先顶了再说','ht6':'感谢分享^_^','ht7':'留名,一会看','ht8':'帮顶、求分!','ht9':'助人为乐^_^','ht10':'精品文章^_^','ht11':'淡淡的路过'}} {'下载':{'ht0':'感谢分享,,,','ht1':'威锋有你更精彩','ht2':' 谢谢了 [s:23]','ht12':'看起来不错啊','ht3':'谢谢楼主了','ht3':'楼主好淫啊,收下了','ht4':'试试看…………','ht5':'先顶了再说','ht6':'感谢分享^_^','ht7':'留名,一会看','ht8':'帮顶、求分!','ht9':'助人为乐^_^','ht10':'精品文章^_^','ht11':'淡淡的路过'}} {'游戏':{'ht0':'感谢分享,,,','ht1':'威锋有你更精彩','ht2':' 谢谢了 [s:24]','ht12':'看起来不错啊','ht3':'谢谢楼主了','ht3':'楼主好淫啊,收下了','ht4':'试试看…………','ht5':'先顶了再说','ht6':'感谢分享^_^','ht7':'留名,一会看','ht8':'帮顶、求分!','ht9':'助人为乐^_^','ht10':'精品文章^_^','ht11':'淡淡的路过'}} {'游戏分享':{'ht0':'感谢分享,,,','ht1':'威锋有你更精彩','ht2':' 谢谢了 [s:25]','ht12':'看起来不错啊','ht3':'谢谢楼主了','ht3':'楼主好淫啊,收下了','ht4':'试试看…………','ht5':'先顶了再说','ht6':'感谢分享^_^','ht7':'留名,一会看','ht8':'帮顶、求分!','ht9':'助人为乐^_^','ht10':'精品文章^_^','ht11':'淡淡的路过'}} {'游戏攻略':{'ht0':'感谢分享,,,','ht1':'威锋有你更精彩','ht2':' 谢谢了 [s:26]','ht12':'看起来不错啊','ht3':'谢谢楼主了','ht3':'楼主好淫啊,收下了','ht4':'试试看…………','ht5':'先顶了再说','ht6':'感谢分享^_^','ht7':'留名,一会看','ht8':'帮顶、求分!','ht9':'助人为乐^_^','ht10':'精品文章^_^','ht11':'淡淡的路过'}} {'游戏机':{'ht0':'感谢分享,,,','ht1':'威锋有你更精彩','ht2':' 谢谢了 [s:27]','ht12':'看起来不错啊','ht3':'谢谢楼主了','ht3':'楼主好淫啊,收下了','ht4':'试试看…………','ht5':'先顶了再说','ht6':'感谢分享^_^','ht7':'留名,一会看','ht8':'帮顶、求分!','ht9':'助人为乐^_^','ht10':'精品文章^_^','ht11':'淡淡的路过'}} {'有声读物':{'ht0':'感谢分享,,,','ht1':'威锋有你更精彩','ht2':' 谢谢了 [s:28]','ht12':'看起来不错啊','ht3':'谢谢楼主了','ht3':'楼主好淫啊,收下了','ht4':'试试看…………','ht5':'先顶了再说','ht6':'感谢分享^_^','ht7':'留名,一会看','ht8':'帮顶、求分!','ht9':'助人为乐^_^','ht10':'精品文章^_^','ht11':'淡淡的路过'}} {'原创汉化':{'ht0':'感谢分享,,,','ht1':'威锋有你更精彩','ht2':' 谢谢了 [s:29]','ht12':'看起来不错啊','ht3':'谢谢楼主了','ht3':'楼主好淫啊,收下了','ht4':'试试看…………','ht5':'先顶了再说','ht6':'感谢分享^_^','ht7':'留名,一会看','ht8':'帮顶、求分!','ht9':'助人为乐^_^','ht10':'精品文章^_^','ht11':'淡淡的路过'}} {'资源':{'ht0':'感谢分享,,,','ht1':'威锋有你更精彩','ht2':' 谢谢了 [s:30]','ht12':'看起来不错啊','ht3':'谢谢楼主了','ht3':'楼主好淫啊,收下了','ht4':'试试看…………','ht5':'先顶了再说','ht6':'感谢分享^_^','ht7':'留名,一会看','ht8':'帮顶、求分!','ht9':'助人为乐^_^','ht10':'精品文章^_^','ht11':'淡淡的路过'}} {'求助':{'ht0':'帮顶了','ht1':'bangding~~','ht2':' 无力。','ht9':'路过,,,','ht3':'这种问题只有求高人了','ht3':'马克一下','ht4':'这种问题...','ht5':'其实你可以论坛搜索试试','ht6':'悲催。。。','ht7':'路过啊。。。顺道帮顶','ht8':'帮顶、求分!','ht9':'助人为乐^_^'}}
附代码:
# -*- coding:utf-8-*- # by sorcerdu import urllib, urllib2, cookielib,re import sys,os,random import mimetypes import mimetools import codecs import time import datetime from urllib2 import HTTPError, URLError def encode_multipart_formdata(fields, files): """ fields is a sequence of (name, value) elements for regular form fields. files is a sequence of (name, filename, value) elements for data to be uploaded as files Return (content_type, body) ready for httplib.HTTP instance """ BOUNDARY = '' CRLF = '\r\n' L = [] for key in fields: L.append('--' + BOUNDARY) L.append('Content-Disposition: form-data; name="%s"' % key) L.append('') L.append(fields[key]) for key in files: L.append('--' + BOUNDARY) L.append('Content-Disposition: form-data; name="'+ key +'"; filename="'+ files[key]['filename'] + '"') L.append('Content-Type: %s' % get_content_type(files[key]['type'])) L.append('') L.append(files[key]['filedata']) L.append('--' + BOUNDARY + '--') L.append('') body = CRLF.join(L) content_type = 'multipart/form-data; boundary=%s' % BOUNDARY return content_type, body def get_content_type(filename): return mimetypes.guess_type(filename)[0] or 'application/octet-stream' class LoginWeiphone: email = '' password = '' cookie = None friendlist = [] def __init__(self, email, passwd): self.userName = email self.password = passwd #cookie self.cookie = cookielib.LWPCookieJar() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cookie)) urllib2.install_opener(opener) def login(self): postdata = { 'Kaf_Model_Form_Login[login]':self.userName, 'Kaf_Model_Form_Login[password]':self.password, 'Kaf_Model_Form_Login[rememberMe]':'0', 'Kaf_Model_Form_Login[rememberMe]':'1' } headers = {'Accept':'*/*', 'Accept-Language':'zh-CN', 'Host': 'bbs.weiphone.com', 'User-Agent':'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C)' } postdata = urllib.urlencode(postdata) #用记登录 req = urllib2.Request('http://passport.weiphone.com/?r=user/loginProcess',postdata) res = urllib2.urlopen(req) result=res.read() res.close() #打开取cookie页面 result = str(result) p_src=(r'(?<=src=")http://bbs.+(?=" r)') src=re.findall(p_src,result) req1 = urllib2.Request(src[0],None,headers) res1 = urllib2.urlopen(req1) result1=res1.read() res1.close() ## print result1 #print result if '''align="absmiddle"''' in result: print u"登录成功。。。" else: print u"登录失败。。。" print result exit(1) def sign(self): headers = {'User-Agent':'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C)' } url = 'http://bbs.weiphone.com/jobcenter.php?action=punch&verify=41607f7a&nowtime=1348973831610&verify=41607f7a' postdata=urllib.urlencode({'step':'2'}) req = urllib2.Request(url,postdata,headers) res= urllib2.urlopen(req) result=res.read() res.close() signstr=re.findall(r'''(?<=":').+(?=',"flag")''',result) print ",".join(signstr).decode('utf-8').encode('gb2312') def get_info(self): do=1 headers = {'Accept':'*/*', 'Accept-Language':'zh-CN', 'Host': 'bbs.weiphone.com', 'User-Agent':'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C)' } while do==1: x=random.randint(5300169,5933162) url='http://bbs.weiphone.com/read-htm-tid-'+repr(x)+'.html' ## url='http://bbs.weiphone.com/read-htm-tid-3491494.html' ## print url p_replay=(r'post.php\?action=reply&fid=\d+&tid=\d+') p_par_time=(r'(?<=title=")\d{4}-\d{2}-\d{2}(?=\s)') p_parinfo=(r'id="anchor">(?:.|[\n])*"h_l">') p_par_1=(r'(?<=name=")(\w+?)" value="(\w*?)"') p_par_2=(r'(?<=value=")(\w*?)" name="(\w*?)"') p_par_3=(u'(?<=id="atc_title" name=")(\w+?)" value="(.+)" size') p_par_type=(u'(?<=class="s5">\[).+(?=\]</a>)') p_par_hexie=(r"(?<=document.FORM._hexie.value = ')\w+(?=')") try: req = urllib2.Request(url,None,headers) res= urllib2.urlopen(req,timeout=20) result=res.read() res.close() if 'showindex' in result: continue else: #匹配发帖时间 sendtime=re.findall(p_par_time,result) sendtime=sendtime[0].replace('-','') sendtime=time.strptime(sendtime,"%Y%m%d") sendday=datetime.datetime(*sendtime[0:6]) #把发帖日期转换为datetime格式 today=datetime.datetime.today() #获取今天的datetime时间 day=today-sendday if day.days >35: #45天前的帖子不回复 continue #匹配回帖链接 url_replay_list=re.search(p_replay,result) ## print url_replay_list.group(0) #匹配回帖主题 atc_title=re.findall(p_par_3,result) #匹配参数段 a=re.findall(p_parinfo,result) if len(a)==0: ## print'此帖已被锁定,换帖中……' continue #匹配参数段A par_1=re.findall(p_par_1,a[0]) #匹配参数段B par_2=re.findall(p_par_2,a[0]) hexie=re.findall(p_par_hexie,result) #匹配帖子类型 par_type=re.findall(p_par_type,result) if len(par_type)!=0: par_type=par_type[0].decode('utf-8') #求助帖3天内回复 if par_type==u'求助' and day.days>5: continue reply_content=self.reply_content(par_type) if reply_content==1: #回复二手区3天内的帖子 if 'fid=29' in url_replay_list.group(0) and day.days<0: reply_content='友情帮顶~'+' '+'[s:'+str(random.randint(1,130))+']' reply_content=reply_content.decode('utf-8').encode('gb2312') else: ## print '类型匹配失败,换帖中……' continue else: ## print '此帖无类型,换帖中……' continue for x in par_2: if 'hexie' not in x[1]: if 'type' not in x[1]: par_1.append((x[1],x[0])) par=[] par_all=par_1+atc_title par_all.append(('_hexie',hexie[0])) for x in par_all: if 'tid' in x[0]: par.append((x[0],x[1])) if 'hexie' in x[0]: par.append((x[0],x[1])) if 'wwsfy' in x[0]: par.append((x[0],x[1])) if 'fid' in x[0]: par.append((x[0],x[1])) if 'verify' in x[0]: par.append((x[0],x[1])) if 'atc_title' in x[0]: par.append((x[0],x[1])) return par,reply_content except HTTPError,e: ## print e.code continue #决定回复内容 def reply_content (self,tz_type): type_name=tz_type.encode('gb2312') info_dic={} file_d=codecs.open('config.txt','r') for line in file_d: str_dic = eval(line.strip()) #将字符串转换成字典 info_dic=dict(info_dic,**str_dic) # 合并字典 if type_name in info_dic.keys(): print u'帖子类型:',tz_type ht_id='ht'+str(random.randint(1,len(info_dic[type_name]))-1) do=random.randint(0,5) if do in list(xrange(5)): return info_dic[type_name][ht_id]+' '+'[s:'+str(random.randint(1,130))+']' else: return info_dic[type_name][ht_id] else: return 1 file_d.close() #提交函数 def reply(self): par_all,reply_content=self.get_info() reply_content=reply_content.decode('gb2312').encode('utf-8') fields={ par_all[0][0]:par_all[0][1], par_all[1][0]:par_all[1][1], par_all[2][0]:par_all[2][1], par_all[3][0]:par_all[3][1], par_all[4][0]:par_all[4][1], par_all[5][0]:par_all[5][1], 'replytouser':'', 'atc_convert':'1', 'atc_autourl':'1', 'step':'2', 'type':'ajax_addfloor', 'cyid':'', 'stylepath':'weiphone', 'ajax':'1', 'iscontinue':'0', 'atc_desc1':'', 'action':'reply', 'atc_content':reply_content, 'atc_desc1':'', 'ok':'1'} test={'test':'1','filename':'','size':'','type':'','filedata':''} files={'file':test} ## post_multipart(host,fields,files) ## print data for cookie in self.cookie: if 'f0e3d_winduser'==cookie.name: val=cookie.value break content_type, body = encode_multipart_formdata(fields, files) headers = {'Accept':'application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*', 'Referer':'http://bbs.weiphone.com/read-htm-tid-5363408.html', 'Accept-Language':'zh-CN', 'Content-Type': content_type, 'Accept-Encoding':'gzip, deflate', 'User-Agent':'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C)', 'Host':'bbs.weiphone.com', 'Content-Length': str(len(body)), 'Connection':'Keep-Alive', 'Pragma':'no-cache', 'Cookie':'f0e3d_winduser='+val} for x in par_all: if 'fid' in x: fid=x[1] if 'verify' in x: verify=x[1] postdata=body header=headers url='http://bbs.weiphone.com/post.php' req = urllib2.Request(url,postdata,headers) res= urllib2.urlopen(req) result=res.read().decode('utf-8') res.close() if self.userName in result: p_par_floor=(u'(?<=title="复制此楼地址">).+(?=楼)') p_par_time=(r'(?<=<span title=").+(?=">)') floor=re.findall(p_par_floor,result) time=re.findall(p_par_time,result) print u'帖子地址:http://bbs.weiphone.com/read-htm-tid-%s.html' % par_all[2][1] print u'帖子标题:',par_all[4][1].decode('utf-8').encode('gb2312') print u'回复内容:',reply_content.decode('utf-8').encode('gb2312') print u'回复楼层:'+str(floor[0])+u' 楼' print u'回复时间:'+str(time[0]) else: print result if __name__ == '__main__': user = LoginWeiphone("username","password") #账号密码 print '==================' user.login() user.sign() print '==================' for x in range(200): #回帖次数 user.reply() print '\n' time.sleep(random.randint(60,200)) print '----------------------------------------' print u'完毕'