#!/usr/bin/env python # -*- coding: utf-8 -*- __author__ = 'poppy' ''' dakele bbs sigin ''' import sys import urllib2 import urllib import requests import cookielib import json from bs4 import beautifulsoup import logging logging.basicconfig(level=logging.debug) reload(sys) sys.setdefaultencoding("utf8") class dakele(object): def __init__(self,name,password): self.name = name self.password = password self.cj = cookielib.lwpcookiejar() self.opener = urllib2.build_opener(urllib2.httpcookieprocessor(self.cj)) urllib2.install_opener(self.opener) def _getheaders(self): headers = {} headers['user-agent']='mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, like gecko) chrome/39.0.2171.95 safari/537.36' #headers['host']='www.dakele.com' headers['connection']='keep-alive' headers['cache-control']='max-age=0' headers['accept-language']='zh-cn,zh;q=0.8,en;q=0.6,zh-tw;q=0.4' #headers['accept-encoding']='gzip, deflate, sdch' headers['accept']='text/html,application/xhtml xml,application/xml;q=0.9,image/webp,*/*;q=0.8' return headers def login(self): '''登录网站''' logging.debug(u'正在登陆 username : %s password : %s' %(self.name,self.password)) logging.debug(u'headers is : %s' % self._getheaders()) loginparams = {'product': 'bbs','surl': r'http://bbs.dakele.com/','username': self.name,'password':self.password,'remember':'0'} logging.debug(u'loginparams is : %s' % loginparams) req = urllib2.request( r'http://passport.dakele.com/logon.do', urllib.urlencode(loginparams), headers=self._getheaders()) response = urllib2.urlopen(req) self.operate = self.opener.open(req) thepage = response.read() result = json.loads(thepage) return result['redirect'] def login_bbs(self,url): '''登录bbs网站''' logging.debug( 'start bbs login : %s ' % url) req = urllib2.request(url,headers=self._getheaders()) response = urllib2.urlopen(req) self.operate = self.opener.open(req) thepage = response.read() #print thepage def _say(self,html): soup = beautifulsoup(html) try: qd_form = soup.find_all(id="qiandao")[0] s_action = qd_form['action'] print 's_action is : %s' %s_action inputes = soup.find_all("input") s_formhash = '' s_qdxq = 'kx' s_qdmode = '1' for input in inputes: if input['name']=='formhash': s_formhash = input['value'] break loginparams = {'formhash':s_formhash,'qdxq':s_qdxq,'qdmode': s_qdmode,'todaysay':u'可乐社区是我家,我们大家都爱ta...'} req = urllib2.request( r'http://bbs.dakele.com/' s_action, urllib.urlencode(loginparams), headers=self._getheaders()) response = urllib2.urlopen(req) self.operate = self.opener.open(req) thepage = response.read() result_soup = beautifulsoup(thepage) for c in result_soup.find_all("div",class_="c"): logging.info(t_text()) except indexerror: logging.info(u'今天已经签到过...') # with open('d:/result.html','w') as fw : # fw.write(thepage) #soup_qdform = beautifulsoup(qd_form.html) #print qd_form.action # d = pq(html) # s_action = d("#qiandao").attr("action") # if s_action: # s_formhash = d("#qiandao input[name=formhash]").attr("value") # s_qdxq = d("#qiandao input[name=qdxq]").attr("value") # s_qdmode = '1' # loginparams = {'formhash':s_formhash,'qdxq':s_qdxq,'qdmode': s_qdmode,'todaysay':u'可乐社区是我家,我们大家都爱ta...'} # req = urllib2.request( r'http://bbs.dakele.com/' s_action, urllib.urlencode(loginparams), headers=self._getheaders()) # response = urllib2.urlopen(req) # self.operate = self.opener.open(req) # thepage = response.read() # else: # logging.debug( u'今天已经签到过...') def sign(self,url): logging.debug( 'start bbs sign : %s' % url) req = urllib2.request(url,headers=self._getheaders()) response = urllib2.urlopen(req) self.operate = self.opener.open(req) thepage = response.read() self._say(thepage) if __name__ == '__main__': userlogin = dakele('xxx','xxx') bbs_loginurl = userlogin.login() userlogin.login_bbs(bbs_loginurl) userlogin.sign('http://bbs.dakele.com/dsu_paulsign-sign.html');
用户登录
还没有账号?立即注册
用户注册
投稿取消
文章分类: |
|
还能输入300字
上传中....