使用Python怎么实现一个爬虫刷回复功能?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
'''
获取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())
先要分析该网站登录地址,登录需要的参数,如上代码
获得cookie之后,分析该论坛的回复参数,该论坛采用的是post提交,需要有帖子id,回复内容等等,分析之后得到如下代码
代码先加载文件里面的cookie,然后调用了haha这个笑话api,当然我已经将这个api的json进行了处理,这里只要笑话内容就行。
'''
回复帖子
'''
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 "当前第" + str(num) + "" + "次回帖"
print("当前帖子id" + str(i))
sleep(1)
发帖子代码:
'''
发帖子(普通帖子或者加悬赏分的帖子:并不是悬赏板块的帖子)
'''
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())
将这些代码进行进行调用就可以去刷回复了。
关于使用Python怎么实现一个爬虫刷回复功能问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。