首页 > 代码库 > 简单的模拟登陆发帖例子
简单的模拟登陆发帖例子
#!/usr/bin/env python # encoding: utf-8 import re import urllib import os,datetime import sys import time from selenium import webdriver from BeautifulSoup import BeautifulSoup from DiscuzAPI import DiscuzAPI def getWorkdays(): holiday_list = [] overtime_list = [] days_list = [] workday_list = [] url = ‘http://www.baidu.com/s?‘ + urllib.urlencode({‘wd‘: ‘日历‘}) date_pattern = re.compile(r‘date="[\d]+[-][\d]+[-][\d]+"‘) driver = webdriver.PhantomJS(service_log_path=os.path.devnull) driver.get(url) html = driver.page_source driver.quit() soup = BeautifulSoup(html) td_div_list = soup.findAll(‘div‘,{‘class‘:‘op-calendar-new-relative‘}) for td_tag in td_div_list: href_tag = str(td_tag.a) date_list = date_pattern.findall(href_tag) if len(date_list) > 0: days_list.append(date_list[0].split(‘"‘)[1]) if href_tag.find(‘休‘) != -1: holiday_list.append(date_list[0].split(‘"‘)[1]) if href_tag.find(‘班‘) != -1: overtime_list.append(date_list[0].split(‘"‘)[1]) for day_format in days_list: day = datetime.datetime.strptime(day_format, ‘%Y-%m-%d‘).date() if day_format in overtime_list: workday_list.append(day) elif day.weekday() not in [5, 6] and day_format not in holiday_list: workday_list.append(day) return workday_list def getRangeThisWeek(workday_list): """获取本周工作日区间""" today = datetime.date.today() if today not in workday_list: # 非工作日,返回空链表 return [] else: for i in range(1,10): pres_day = today - datetime.timedelta(days=i) if pres_day not in workday_list: startday = pres_day + datetime.timedelta(days=1) break for i in range(1,10): next_day = today + datetime.timedelta(days=i) if next_day not in workday_list: endday = next_day - datetime.timedelta(days=1) break return [startday, endday] if __name__ == ‘__main__‘: # 从百度“日历”获取工作日列表 workday_list = getWorkdays() workday_range = getRangeThisWeek(workday_list) url = "http://bbs.cnfol.wh" username = "report" password = "eQIi}38" robot = DiscuzAPI(url, username, password) robot.login() # 发日报主题贴 fid_wh = 52 fid_fz = 72 msg = u"格式:\n\"\"\"\n今日工作内容:\n\n明日工作计划:\n\n\"\"\"" today = datetime.date.today() subject = today.strftime(‘%Y%m%d‘).encode(‘utf-8‘) if today in workday_list: # 检查主题是否已经存在了,武汉 result = robot.isSubExisted(fid = fid_wh, subject = subject) if result == False: print "wh主题贴发布:" + subject robot.publish(fid = fid_wh, subject = subject, msg = msg) else: print "wh主题已存在,不重复发布:" + subject # discuz 发贴时间间隔,默认15s #time.sleep() # 福州日报 result = robot.isSubExisted(fid = fid_fz, subject = subject) if result == False: print "fz主题贴发布:" + subject robot.publish(fid = fid_fz, subject = subject, msg = msg) else: print "fz主题已存在,不重复发布:" + subject # 发周报主题贴 fid_wh_week = 51 fid_fz_week = 73 msg = u"参照[url=http://bbs.cnfol.wh/forum.php?mod=viewthread&tid=20&extra=page%3D1]周报规范[/url]进行回复。" if len(workday_range) > 1: subject = workday_range[0].strftime(‘%Y%m%d‘).encode(‘utf-8‘) + "-" + workday_range[1].strftime(‘%Y%m%d‘).encode(‘utf-8‘) result = robot.isSubExisted(fid = fid_wh_week, subject = subject) if result == False: print "wh_week主题贴发布:" + subject robot.publish(fid=fid_wh_week, subject = subject, msg = msg) else: print "wh_week主题贴已存在,不重复发布:" + subject result = robot.isSubExisted(fid = fid_fz_week, subject = subject) if result == False: print "fz_week主题贴发布:" + subject robot.publish(fid=fid_fz_week, subject = subject, msg = msg) else: print "fz_week主题贴已存在,不重复发布:" + subject
#! /usr/bin/env python # -*- coding: utf-8 -*- """ base by Conanca image upload by N3il """ import urllib2 import urllib import cookielib import random import string import re import time import sys import httplib import mimetools import mimetypes httplib.HTTPConnection.debuglevel = 1 class DiscuzAPI: def __init__(self, forumUrl, userName, password, proxy = None): ‘‘‘ 初始化论坛url、用户名、密码和代理服务器 ‘‘‘ self.forumUrl = forumUrl self.userName = userName self.password = password self.formhash = ‘‘ self.isLogon = False self.isSign = False self.xq = ‘‘ self.jar = cookielib.CookieJar() if not proxy: openner = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.jar)) else: openner = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.jar), urllib2.ProxyHandler({‘http‘ : proxy})) urllib2.install_opener(openner) def login(self): ‘‘‘ 登录论坛 ‘‘‘ url = self.forumUrl + "/member.php?mod=logging&action=login&loginsubmit=yes&infloat=yes&inajax=1"; postData = http://www.mamicode.com/urllib.urlencode({‘username‘: self.userName, ‘password‘: self.password, ‘answer‘: ‘‘, ‘cookietime‘: ‘2592000‘, ‘handlekey‘: ‘ls‘, ‘questionid‘: ‘0‘, ‘quickforward‘: ‘yes‘, ‘fastloginfield‘: ‘username‘})"hidden\" name=\"formhash\" value=http://www.mamicode.com/"(.*?)\" />‘, content) if len(rows)!=0: self.formhash = rows[0] #print ‘formhash is: ‘ + self.formhash else: print ‘none formhash!‘ rows = re.findall(r‘<input id=.* type=\"radio\" name=\"qdxq\" value=http://www.mamicode.com/"(.*?)\" style=\"display:none\">‘, content) if len(rows)!=0: self.xq = rows[0] print ‘xq is: ‘ + self.xq elif u‘已经签到‘ in content: self.isSign = True print ‘signed before!‘ else: #print ‘none xq!‘ pass def reply(self, tid, subject = u‘‘,msg = u‘支持~~~顶一下下~~嘻嘻‘): ‘‘‘ 回帖 ‘‘‘ url = self.forumUrl + ‘/forum.php?mod=post&action=reply&fid=41&tid=‘+str(tid)+‘&extra=page%3D1&replysubmit=yes&infloat=yes&handlekey=fastpost&inajax=1‘ postData = http://www.mamicode.com/urllib.urlencode({‘formhash‘: self.formhash, ‘message‘: msg.encode(‘utf-8‘), ‘subject‘: subject.encode(‘gbk‘), ‘posttime‘:int(time.time()) })""): ‘‘‘ 发帖 ‘‘‘ url = self.forumUrl + ‘/forum.php?mod=post&action=newthread&fid=‘+ str(fid) +‘&extra=&topicsubmit=yes‘ """ formhash=d649673a&posttime=1367460177&wysiwyg=1&subject=test&unused%5B%5D=70554 &message=tset123214141&save=&attachnew%5B70555%5D%5Bdescription%5D=&usesig=1&allownoticeauthor=1 """ postData = http://www.mamicode.com/urllib.urlencode("", ‘uploadalbum‘:‘‘, ‘usesig‘:‘1‘, ‘wysiwyg‘:‘0‘ }) req = urllib2.Request(url,postData) content = urllib2.urlopen(req).read().decode(‘utf-8‘, ‘ignore‘) if subject in content: print ‘publish success!‘ return 1 else: print ‘publish faild!‘ return 0 def sign(self,msg = u‘哈哈,我来签到了!‘): ‘‘‘ 签到 ‘‘‘ if self.isSign: return if self.isLogon and self.xq: url = self.forumUrl + ‘/plugin.php?id=dsu_paulsign:sign&operation=qiandao&infloat=1&inajax=1‘ postData = http://www.mamicode.com/urllib.urlencode({‘fastreply‘: ‘1‘, ‘formhash‘: self.formhash, ‘qdmode‘: ‘1‘, ‘qdxq‘: self.xq, ‘todaysay‘:msg.encode(‘utf-8‘) })"/forum.php?mod=post&action=newthread&fid=%d&extra=" % fid data = http://www.mamicode.com/urllib2.urlopen(url).read().decode(‘utf-8‘, ‘ignore‘)"<input type=\"hidden\" name=\"hash\" value=http://www.mamicode.com/"(.*?)\">", re.S) uidReg = re.compile(r"discuz_uid = ‘(.*?)‘", re.S) hashRet = hashReg.search( data ).group(1) uid = uidReg.search( data ).group(1) # Upload the image uploadImageUrl = self.forumUrl + "/misc.php?mod=swfupload&operation=upload&simple=1&type=image" refer = self.forumUrl + "/forum.php?mod=post&action=newthread&fid=%d&extra=" % fid randomStr = "7dd" + ‘‘.join( random.sample(string.ascii_lowercase + string.digits, 8) ) CRLF = ‘\r\n‘ #BOUNDARY = mimetools.choose_boundary() BOUNDARY = "---------------------------" + randomStr L = [] L.append(‘--‘ + BOUNDARY) L.append("Content-Disposition: form-data; name=\"uid\"" ) L.append("") L.append(uid) L.append(‘--‘ + BOUNDARY) L.append(‘Content-Disposition: form-data; name=\"hash\"‘) L.append("") L.append(hashRet) L.append(‘--‘ + BOUNDARY) L.append(‘Content-Disposition: form-data; name=\"Filedata\"; filename=\"testpic.jpg\"‘) L.append("Content-Type: image/pjpeg") L.append("") L.append( imageData ) L.append(‘--‘ + BOUNDARY + ‘--‘) L.append("") postData = http://www.mamicode.com/CRLF.join(str(a) for a in L)"/forum.php?mod=forumdisplay&fid=%d" % fid content = urllib2.urlopen(url).read() subject = ">" + subject + "<" if subject.encode(‘utf-8‘) in content: return True else: return False if __name__ == ‘__main__‘: url = "http://bbs.cnfol.wh" username = "zhangzhao" password = "zhangzhao" fid = 59 robot = DiscuzAPI(url, username, password) robot.login() print robot.isSubExisted(fid=fid, subject=u‘20170505‘)
简单的模拟登陆发帖例子
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。