Python实现某论坛自动签到功能

yipeiwu_com5年前Python基础

1.[文件] DakeleSign.py ~ 4KB

#!/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 pyquery import PyQuery as pq
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):
    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('username','password')
  bbs_loginurl = userlogin.login()
  userlogin.login_bbs(bbs_loginurl)
  userlogin.sign('http://bbs.dakele.com/dsu_paulsign-sign.html');

总结

以上所述实小编给大家介绍的Python实现某论坛自动签到功能,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

相关文章

利用Python获取赶集网招聘信息前篇

如何获取一个网站的相关信息,获取赶集网的招聘信息,本文为大家介绍利用python获取赶集网招聘信息的关键代码,供大家参考,具体内容如下 import re import urllib...

下载与当前Chrome对应的chromedriver.exe(用于python+selenium)

下载与当前Chrome对应的chromedriver.exe(用于python+selenium)

一、 打开Chrome浏览器,输chrome://version/ 二、下载chromedriver.exe驱动 注意:上图可以看到安装的Chrome浏览器版本为79.0.3945.8...

Pycharm中Python环境配置常见问题解析

Pycharm中Python环境配置常见问题解析

本文实例讲述了Pycharm中Python环境配置常见问题。分享给大家供大家参考,具体如下: 1、问题的发现 最近在用Pycharm下的命令行工具安装、运行jupyter noteboo...

python pip源配置,pip配置文件存放位置的方法

pip源配置文件可以放置的位置: Linux/Unix: /etc/pip.con ~/.pip/pip.conf (每一个我都找了都没有,所以我是在这个文件夹中创建的pip.conf文...

python对html过滤处理的方法

如下所示: ##过滤HTML中的标签 #将HTML中标签等信息去掉 #@param htmlstr HTML字符串. def filter_tags(htmlstr): #先过滤C...