您好,登錄后才能下訂單哦!
使用Python怎么實(shí)現(xiàn)一個(gè)爬蟲刷回復(fù)功能?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。
''' 獲取cookie ''' def get_cookie(login_data, url, testurl=None): filename = "cookie" cookie = cookielib.MozillaCookieJar(filename) hadler = urllib2.HTTPCookieProcessor(cookie) opener = urllib2.build_opener(hadler) post_data = urllib.urlencode( {'logname': 123456, "logpass": "123456", "action": "login", }) url = 'http://*****.me/waplogin.aspx' opener.open(url, post_data) cookie.save(ignore_discard=True, ignore_expires=True) print("獲取成功") # print(opener.open(testurl).read())
先要分析該網(wǎng)站登錄地址,登錄需要的參數(shù),如上代碼
獲得cookie之后,分析該論壇的回復(fù)參數(shù),該論壇采用的是post提交,需要有帖子id,回復(fù)內(nèi)容等等,分析之后得到如下代碼
代碼先加載文件里面的cookie,然后調(diào)用了haha這個(gè)笑話api,當(dāng)然我已經(jīng)將這個(gè)api的json進(jìn)行了處理,這里只要笑話內(nèi)容就行。
''' 回復(fù)帖子 ''' def post_reply(): filename = "cookie" cookie = cookielib.MozillaCookieJar(filename) cookie.load(filename, ignore_discard=True, ignore_expires=True) handler = urllib2.HTTPCookieProcessor(cookie) opener = urllib2.build_opener(handler) num=0 for i in range(216255, 800000): num = num + 1 huifu = urllib.urlencode( {'sendmsg': 0, "content": str(haha(num)), "action": "add", "id": str(i), "classid": 177}) gradeUrl = 'http://******.me/bbs/book_re.aspx' result = opener.open(gradeUrl) print result.read() print "當(dāng)前第" + str(num) + "" + "次回帖" print("當(dāng)前帖子id" + str(i)) sleep(1)
發(fā)帖子代碼:
''' 發(fā)帖子(普通帖子或者加懸賞分的帖子:并不是懸賞板塊的帖子) ''' def post_articles(book_title, book_content, classid=177, sendmoney=0): filename = "cookie" cookie = cookielib.MozillaCookieJar(filename) cookie.load(filename, ignore_discard=True, ignore_expires=True) handler = urllib2.HTTPCookieProcessor(cookie) opener = urllib2.build_opener(handler) post_articles = urllib.urlencode( {'sendmsg': 0, "book_title": str(book_title), "action": "gomod", "siteid": "1000", "book_content": str(book_content), "classid": classid, "sendmoney": sendmoney}) gradeUrl = 'http://*****.me/bbs/book_view_add.aspx' result = opener.open(gradeUrl, post_articles) print(result.read())
將這些代碼進(jìn)行進(jìn)行調(diào)用就可以去刷回復(fù)了。
關(guān)于使用Python怎么實(shí)現(xiàn)一個(gè)爬蟲刷回復(fù)功能問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。