python实现某论坛自动签到bs4解析html文档

python实现某论坛自动签到bs4解析html文档,第1张

概述python实现某论坛自动签到bs4解析html文档

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

#!/usr/bin/env python# -*- Coding: utf-8 -*-__author__ = 'poppy''''dakele bbs sigin'''import sysimport urllib2import urllibimport requestsimport cookielibimport Jsonfrom bs4 import BeautifulSoupimport logginglogging.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,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,'todaysay':u'可乐社区是我家,我们大家都爱Ta...'}        #             req = urllib2.Request( r'http://bbs.dakele.com/'+s_action,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');

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的python实现某论坛自动签到bs4解析html文档全部内容,希望文章能够帮你解决python实现某论坛自动签到bs4解析html文档所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1199342.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存