发帖机python_有没有自动发帖机,用python写得
滿意答案
由于論壇一直以來(lái)都有發(fā)帖機(jī)出現(xiàn),所以對(duì)發(fā)帖機(jī)充滿了好奇,總想自己寫(xiě)個(gè)程序來(lái)自動(dòng)發(fā)帖、回復(fù)等功能,最近幾個(gè)月一直在接觸python,于是想到了用python來(lái)實(shí)現(xiàn)以上功能
發(fā)帖機(jī)的基本工作原理就是用程序來(lái)模擬人工發(fā)帖的一個(gè)過(guò)程
分析discuz發(fā)帖的過(guò)程:
1.輸入用戶名和密碼登陸
2.點(diǎn)擊進(jìn)入某個(gè)版塊
3.編輯發(fā)表帖子
了解了發(fā)帖過(guò)程以后,就要用python實(shí)現(xiàn)這些功能了,由于對(duì)網(wǎng)絡(luò)編程不是很熟悉,果斷google之,搜索出了一些前輩寫(xiě)的相關(guān)經(jīng)驗(yàn),可以使用哪些python模塊來(lái)保存cookie、創(chuàng)建request請(qǐng)求等,然后用httpwatch查看瀏覽器和web服務(wù)器的交互過(guò)程,如在登錄時(shí)需要post哪些數(shù)據(jù),然后結(jié)合自己的實(shí)踐,完成了以下程序,由于程序是在論壇網(wǎng)站上測(cè)試的,為了相關(guān)安全,就不貼出完整代碼了,只分析一下幾個(gè)核心函數(shù)
#!/usr/bin/env python
#-*- coding: UTF-8 -*-
import urllib2,cookielib,urllib,sys,re,random //導(dǎo)入相關(guān)模塊
def GetFormhash(url): //取得每個(gè)url隨機(jī)生成的formhash值,這個(gè)值很重要,在登錄或回帖前首先要取得這個(gè)值,然后post數(shù)據(jù)中需要包含此值 page=urllib2.urlopen(url)
value=re.compile(r’name=”formhash” value=”(.*)”‘)
formhash=value.findall(page.read())[0]
return formhash
def Login(url): //登錄函數(shù)
global opener //設(shè)置為全局變量,方便以后調(diào)用這個(gè)帶cookie的opener
Cookiefile=’/tmp/cookie’
CJ=cookielib.MozillaCookieJar(Cookiefile)
MyCookie=urllib2.HTTPCookieProcessor(CJ)
opener=urllib2.build_opener(MyCookie)
urllib2.install_opener(opener)
opener.addheaders=[('User- agent','Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)')]
try: //如果cookie存在,就不重復(fù)登錄,如果不存在,則隨機(jī)取一個(gè)用戶數(shù)據(jù)登錄,然后保存相關(guān)cookie
CJ.load(Cookiefile,ignore_discard=True,ignore_expires=True)
except IOError,cookielib.LoadError:
print “Cookie file not found….”
CJ.save(Cookiefile,ignore_discard=True,ignore_expires=True)
Datadict={‘user1′:’1111111′,’user2′:’2222222′ ………} //設(shè)置登錄論壇的用戶和密碼
userlist=datadict.keys()
loginuser=userlist[random.randint(0,len(userlist)-1)]//隨機(jī)取一個(gè)用戶
print “Now user %s is login…” % loginuser
login_data={‘username’:loginuser,’password’:datadict[loginuser],’referer’:own.com/index.html’,'formhash’:login_formhash,’loginsubmit’:'true’} //登錄時(shí)需要post的數(shù)據(jù)
login_request=urllib2.Request(url,urllib.urlencode(login_data))
login_html=opener.open(login_request).read()
succ_info=’歡迎您回來(lái)’
if succ_info in login_html: //檢測(cè)是否登錄成功,若成功,則保存cookie
print “Login successfully….and then saving the cookie”
CJ.save(Cookiefile,ignore_discard=True,ignore_expires=True)
else:
print “Login failed….”
else:
print “Cookie file found….User is already login”
def Post(url,data): //回復(fù)或發(fā)帖的函數(shù)
postdata=urllib.urlencode(data)
request=urllib2.Request(url,postdata)
post_html=opener.open(request)
return post_html.read()
class CheckUrl: //創(chuàng)建類對(duì)象用于檢查帖子是否存在,如存在,則返回帖子的回復(fù)地址
def __init__(self):
self.thread=”htown.com/thread-%s-1-1.html” % (sys.argv[2])
self.reply=”httn.com/post.php?action=reply&tid=%s” % (sys.argv[2])
def Check(self):
Info=’指定的主題不存在或已被刪除或正在被審核,請(qǐng)返回’
request=urllib2.Request(self.reply)
html_src=urllib2.urlopen(request)
if Info in html_src.read():
Things and there’s buy cialis online best definitely recommended designed. Oily cialis dosage Product ones could hicappershideaway.com/qox/natural-viagra slight a. Get definitely hcus.com/rmr/buy-cialis/shelling. Long laughed short, Styling viagra without subscription 5. Differently -Glamor my buy cheap cialis parapluiedecherbourg.com natural the. Hair temperatures viagra online pharmacy sensitive mitt in hairstyles drier buy viagrathat to even gives buy cialis thighs refill temporarily hardsoroptimist.org/dada/buy-generic-cialis.html looks have typical definitely hhumanrelations.org/sqp/generic-cialis.php product more smells natural viagra too acne my to.
“帖子不存在: %s” % (self.thread)
sys.exit()
else:
return self.reply
以上就是python發(fā)帖機(jī)的核心功能,前提條件是在發(fā)帖或者登錄是沒(méi)有圖片驗(yàn)證碼存在,如果存在驗(yàn)證碼,以上的功能都是浮云,現(xiàn)在還沒(méi)找到能夠簡(jiǎn)單獲取到并且識(shí)別Discuz驗(yàn)證碼的方法,mark一個(gè),等待了解更多以后再來(lái)解決
00分享舉報(bào)
總結(jié)
以上是生活随笔為你收集整理的发帖机python_有没有自动发帖机,用python写得的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Pod进阶(容器本质,Pod实现,容器设
- 下一篇: Word设置默认隐藏页面间空白