Holmesian Blog

Python自动签到街旁

之前街旁连续登录签到130多天,结果由于有一天比较忙忘记登录签到导致连续加分的内容没有了,今天稍微花了一点时间用Python实现自动登录并签到,挂在服务器上每天cron一次。


    #coding=gb2312
    import urllib2,cookielib,os,sys,urllib
    
    class AutoloadingLogin():
      def __init__ (self,codeurl,userdata,loginurl,validatename,codesavepath,posturl,postdata):
        self.codeurl = codeurl
        self.userdata = userdata
        self.loginurl = loginurl
        self.validatename = validatename
        self.opener = None
        self.codesavepath = codesavepath
        self.postdata=postdata
        self.posturl=posturl
    
      def savecode (self,content,imagetype):
        t = imagetype.split("/")
        h = open(("%svalidate.%s" %(self.codesavepath,t[1])),"wb")
        h.write(content)
        h.close()
    
      def request (self,url,cj=None,data=None):
        request=urllib2.Request(url)
        if cj!=None:
          self.opener=urllib2.build_opener(cj)
        if data!=None :
          c = self.opener.open(request,urllib.urlencode(data))
        else:
          c = self.opener.open(request)
        return c
    
      def login (self):
        cookie=cookielib.CookieJar()
        cj=urllib2.HTTPCookieProcessor(cookie)
        if self.validatename != '':
          c = self.request(self.codeurl,cj)
          imagetype = c.headers.get('Content-Type')
          self.savecode (c.read(),imagetype)
          print "请输入验证码"
          imgcode=raw_input()
          self.userdata[self.validatename] = imgcode
        else:
          print "无验证码"
        c = self.request(self.loginurl,cj,self.userdata)
        self.postin()
      
      def postin (self):
        c = self.request(self.posturl,None,self.postdata)
        #print c.read().decode("utf-8")
    
    def main ():
      #验证码url,如果有的话
      validatecodeurl = ""
      #提交登录或注册时的表单
      userdata = {'pwd':'justyourpasswd','user':'Holmesian@qq.com','sumbit':'登录'}
      #验证码的表单变量,如果有的话
      validatename = ''
      #登录表单提交Url
      loginurl = 'http://jiepang.com/m/login'
      #下载的验证码路径
      codesavepath = ''
      #设置签到变量
      posturl = 'http://jiepang.com/m/checkin?sid=4e0d3d1e089hwt9ejouy'
      postdata = {'douban':'on','fanfou':'on','id':'0B5F8533A77CC23CC7B953DD11A8D30A','kaixin001':'on','qq':'on','renren':'on','sina':'on','status':''}
      a = AutoloadingLogin(validatecodeurl,userdata,loginurl,validatename,codesavepath,posturl,postdata)
      #登录并签到
      a.login()
    
    if __name__ == '__main__':
      main()

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »