[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[原创代码] [更新]Python:百度贴吧签到器

本帖最后由 canyuexiaolang 于 2013-7-8 07:55 编辑

喵,由于高考报志愿神马的一直没来论坛,现在都捣鼓完了,我出山了= =.
百度贴吧签到器signBaidu更新了又,增加了[伪]多线程签到的功能。大家可以参考此代码学习python。

用法如下:
signBaidu v2.1 by Ricter

Python signBaidu.py username password [thread]

-------------------------------------------------------------------
Linux:[ricter@Ricter ~]# python signBaidu.py ricter 123456 thread
Windows:C:\Users\Ricter\>python signBaidu.py batman 654321
-------------------------------------------------------------------

Python跨平台感觉真爽...
代码如下:
  1. # -*- coding:utf-8 -*-
  2. #-----------------------------------------------
  3. #signBaidu 2.1 by Ricter 2013-06-14
  4. #E-mail:canyuexiaolang@gmail.com
  5. #Website:http://ricter.rp2.jybox.net
  6. #-----------------------------------------------
  7. import re,time,os
  8. import urllib,urllib2
  9. import threading,sys
  10. def CheckUpdate():
  11.     checkupdate = signBaidu()
  12.     version = 'v2.1'
  13.     checkupdate.getURL('http://ricter.rp2.jybox.net/download/python/signBaidu/update.txt', None)
  14.     if checkupdate.requestData.strip('\n').split(',')[0] == version:
  15.         print 'signBaidu is already the newest version.'
  16.         main(isthread)
  17.     elif checkupdate.requestData.strip('\n').split(',')[1] == 'important':
  18.         print 'signbaidu %s need to be updated (signBaidu %s), please download from http://ricter.rp2.jybox.net/download/python/signBaidu' % (version, checkupdate.requestData.strip('\n').split(',')[0])
  19.     else:
  20.         print 'signBaidu %s is not the newest version(%s).' % (version, checkupdate.requestData.strip('\n').split(',')[0])
  21.         main(isthread)
  22. class signBaidu(object):
  23.     mainUrl  = 'http://wapp.baidu.com'
  24.     loginUrl = 'http://wappass.baidu.com/passport/'
  25.     username = ''
  26.     password = ''
  27.     vcodestr = ''
  28.     vcode    = ''
  29.     tbUrl    = ''
  30.     result   = ''
  31.     barlist  = []
  32.     faillist = []
  33.     uid = ''
  34.     postDict = {
  35.         'username'    : '',
  36.         'password'    : '',
  37.         'submit'      : '登录',
  38.         'isphone'     : '0',
  39.         'vcodestr'    : '',
  40.         'u'           : 'http%3A%2F%2Fwap.baidu.com%3Fuid%3D',
  41.         'tpl'         : '',
  42.         'ssid'        : '',
  43.         'from'        : '',
  44.         'uid'         : '',
  45.         'pu'          : '',
  46.         'tn'          : '',
  47.         'bdcm'        : '',
  48.         'type'        : '',
  49.         'bd_page_type': ''
  50.     }
  51.     requestData = ''
  52.     def showusrandpwd(self):
  53.         """Show Username and Password"""
  54.         #print 'in showusrandpwd'
  55.         print '-' * 40
  56.         print 'Username:',self.username, '\nPassword:',self.password
  57.         print '-' * 40
  58.     def getURL(self,url,data):
  59.         """Open webpages and get the HTML code"""
  60.         #print 'in getURL'
  61.         req = urllib2.Request(url, data)
  62.         req.add_header('Content-Type', 'application/x-www-form-urlencoded')
  63.         resp = urllib2.urlopen(req)
  64.         #print 'Get Url:', url
  65.         self.requestData = resp.read()
  66.     def getLoginUid(self):
  67.         """Get Login Uid"""
  68.         #print 'in getLoginUid'
  69.         resp = urllib2.urlopen(self.loginUrl)
  70.         uid = re.search(r'[_0-9]{17}', resp.read())
  71.         if uid:
  72.             self.uid = uid.group()
  73.             print 'Get uid success:', self.uid
  74.         else:
  75.             print  'Get uid fail.'
  76.     def checkIsUidGet(self):
  77.         """Check self.uid is defineded or not"""
  78.         #print 'in checkIsUidGet'
  79.         if self.uid:
  80.             return True
  81.         else:
  82.             return False
  83.     def loginBaidu(self):
  84.         """Login Baidu bar and get infomation"""
  85.         #print 'in loginBaidu'
  86.         self.getLoginUid()
  87.         if self.checkIsUidGet():
  88.             self.postDict['username'] = self.username
  89.             self.postDict['password'] = self.password
  90.             self.postDict['uid'] = self.uid
  91.             self.postDict['u'] = self.postDict['u'] + self.uid
  92.             #print self.postDict
  93.             postData = urllib.urlencode(self.postDict)
  94.             self.getURL(self.loginUrl, postData)
  95.             #print self.requestData
  96.             self.result = self.getLoginRequest()
  97.             if self.result == '0':
  98.                 postData = urllib.urlencode(self.postDict)
  99.                 self.getURL(self.loginUrl, postData)
  100.                 self.result = self.getLoginRequest()
  101.             elif self.result == '1':
  102.                 print 'Login FAIL!'
  103.         else:
  104.             print 'Construct Post data fail.'
  105.     def getLoginRequest(self):
  106.         """Get infomation from login page"""
  107.         #print 'in getLoginRequest'
  108.         if "请您输入验证码" in self.requestData:
  109.             for i in range(0,len(self.requestData.split('\n'))):
  110.                 if '验证码:' in self.requestData.split('\n')[i]:
  111.                     vcodeimgUrl = self.requestData.split('\n')[i].split('<')[2].split('=')[1].split(' ')[0].strip('"')
  112.                     self.vcodestr = vcodeimgUrl.split('?')[1]
  113.                     print '-' * 79
  114.                     print vcodeimgUrl
  115.                     print '-' * 79
  116.                     self.vcode = raw_input('Verification code:')
  117.                     self.postDict['vcodestr'] = self.vcodestr
  118.                     self.postDict['verifycode'] = self.vcode
  119.                     #print self.postDict
  120.                     return '0'
  121.         elif "您输入的密码有误" in self.requestData:
  122.             print 'Password WRONG!'
  123.             #print self.requestData
  124.             return '1'
  125.         elif "您输入的验证码有误" in self.requestData:
  126.             print 'Verifycode WRONG!'
  127.             #print self.requestData
  128.             return '1'
  129.         elif "系统错误" in self.requestData:
  130.             print 'Unexpect Error!'
  131.             #print self.requestData
  132.             return '1'
  133.         else:
  134.             for line in self.requestData.split('<'):
  135.                 if 'itj=23' in line:
  136.                     self.tbUrl = line.split('"')[1].replace('amp;', '')
  137.                     print 'Get Baidu Bar Url...Success!'
  138.             #print self.requestData
  139.     def getBar(self):
  140.         """Get Baidu Bar List"""
  141.         #print 'in getBar'
  142.         favoUrl = ''
  143.         self.getURL(self.tbUrl, None)
  144.         #print self.requestData
  145.         for line in self.requestData.split('<'):
  146.             if 'tab=favorite' in line:
  147.                 favoUrl = self.mainUrl + line.split('"')[1]
  148.                 break
  149.         if favoUrl:
  150.             self.getURL(favoUrl, None)
  151.             #print self.requestData
  152.             for line in self.requestData.split('<'):
  153.                 if 'm?kw' in line:
  154.                     tmp_tbUrl = favoUrl.split('?')[0] + "?" + line.split('"')[1].split('?')[1]
  155.                     self.barlist.append(tmp_tbUrl.replace('amp;', ''))
  156.                     #print tmp_tbUrl.replace('amp;', '')
  157.     def signBar(self):
  158.         """Sign in Baidu Bar"""
  159.         #print 'in signBar'
  160.         if self.checkBar():
  161.             print 'OK!'
  162.             print '-' * 40
  163.             print 'Baidu Bar Sign starting...'
  164.             for url in self.barlist:
  165.                 self.getURL(url, None)
  166.                 for line in self.requestData.split('<'):
  167.                     if 'sign?' in line:
  168.                         self.getURL(self.mainUrl + line.split('"')[1].replace('amp;',''),None)
  169.                         break
  170.                 for i in range(len(self.requestData.split('/'))):
  171.                     if 'name="top"' in self.requestData.split('/')[i]:
  172.                         temp_info = self.requestData.split('/')[i + 1]
  173.                         break
  174.                 if '签到成功' in temp_info:
  175.                     print 'Success:',urllib.unquote(url.split('=')[1]).decode('utf8'),\
  176.                                      temp_info.split('>')[2].split('<')[0].decode('utf8') +\
  177.                                      temp_info.split('>')[3].strip('<').decode('utf8')
  178.                 else:
  179.                     print 'Fail:',urllib.unquote(url.split('=')[1]).decode('utf8')
  180.                     self.faillist.append(url)
  181.             print '-' * 40
  182.         else:
  183.             print 'Fail:No favourite bar.'
  184.     def showBarName(self, bar_list):
  185.         """Show Bar Name"""
  186.         #print 'in showBarName'
  187.         print '-' * 40
  188.         for url in bar_list:
  189.             print urllib.unquote(url.split('=')[1]).decode('utf8')
  190.         print '-' * 40
  191.     def checkBar(self):
  192.         """Check Bar list"""
  193.         #print 'in checkBar'
  194.         if self.barlist:
  195.             return True
  196.         else:
  197.             return False
  198.     #--------------------------------------------------------------------------------
  199.     def getURL_threading(self, url, data):
  200.         """Open webpages and get the HTML code by each threading"""
  201.         #print 'in getURL'
  202.         req = urllib2.Request(url, data)
  203.         req.add_header('Content-Type', 'application/x-www-form-urlencoded')
  204.         resp = urllib2.urlopen(req)
  205.         #print 'Get Url:', url
  206.         return resp.read()
  207.     def signBar_threading(self, barUrl):
  208.         """Sign in Baidu Bar by using threading"""
  209.         resp = self.getURL_threading(barUrl, None)
  210.         for line in resp.split('<'):
  211.             if 'sign?' in line:
  212.                 #print self.mainUrl + line.split('"')[1].replace('amp;','')
  213.                 resp = self.getURL_threading(self.mainUrl + line.split('"')[1].replace('amp;',''),None)
  214.                 break
  215.         for i in range(len(resp.split('/'))):
  216.             if 'name="top"' in resp.split('/')[i]:
  217.                 temp_info = resp.split('/')[i + 1]
  218.                 break
  219.         if '签到成功' in temp_info:
  220.             print 'Success:',urllib.unquote(barUrl.split('=')[1]).decode('utf8'),\
  221.                              temp_info.split('>')[2].split('<')[0].decode('utf8') +\
  222.                              temp_info.split('>')[3].strip('<').decode('utf8')
  223.         else:
  224.             print 'Fail:', urllib.unquote(barUrl.split('=')[1]).decode('utf8')
  225.             self.faillist.append(barUrl)
  226.     def sign_threading(self):
  227.         worker = []
  228.         names = locals()
  229.         print 'OK!'
  230.         print '-' * 40
  231.         print 'Baidu Bar Sign starting...'
  232.         for i in range(0,len(self.barlist)):
  233.             names['t%d' % i]  = threading.Thread(target=self.signBar_threading,args=(self.barlist[i],))
  234.             names['t%d' % i].start()
  235.             worker.append(names['t%d' % i])
  236.             if i % 5 == 0 and i > 0:
  237.                 print 'Thread-' + str(i / 5) + ' Starting...'
  238.                 for row in worker:
  239.                     row.join()
  240.                 worker = []
  241.     #--------------------------------------------------------------------------------
  242. def main(isthread):
  243.     login = signBaidu()
  244.     login.showusrandpwd()
  245.     login.loginBaidu()
  246.     if login.tbUrl:
  247.         print 'Successful login!'
  248.         print 'Get Baidu Bar List...',
  249.         login.getBar()
  250.         if login.barlist:
  251.             if isthread:
  252.                 login.sign_threading()
  253.             else:
  254.                 login.signBar()
  255.         if login.faillist:
  256.             print 'The following Baidu Bar sign fails, re-sign start...'
  257.             login.barlist = login.faillist
  258.             login.sign_threading()
  259. try:
  260.     isthread = sys.argv[3]
  261. except:
  262.     isthread = ''
  263. try:
  264.     signBaidu.username = sys.argv[1]
  265.     signBaidu.password = sys.argv[2]
  266. except:
  267.     print '\nsignBaidu v2.1 by Ricter\n'
  268.     print 'Python signBaidu.py username password [thread]\n'
  269.     print '-'*67
  270.     print ' Linux:[ricter@Ricter ~]# python signBaidu.py ricter 123456 thread'
  271.     print ' Windows:C:\Users\Ricter\>python signBaidu.py batman 654321'
  272.     print '-'*67
  273. else:
  274.     CheckUpdate()
  275. #main(isthread)
复制代码


教程:http://blog.ricter.rp2.jybox.net/article/2013-04-03-21-00
来源:http://ricter.rp2.jybox.net/
1

评分人数

    • Batcher: 感谢分享PB + 10 技术 + 1

能否举个例子,比如我要在批处理吧签到,应该如何设置?

TOP

牛逼啊,Python这个也是VBS语言?

TOP

按键精灵 那种类型的 咱能编出一个来,
这种只能膜拜了。好像 签到 没有验证码之类的东西出来吧。

TOP

楼主不见了?2楼问题求解

TOP

回复 2# BAT-VBS
def main():

mainurl = 'http://wapp.baidu.com'

loginurl = 'http://wappass.baidu.com/passport/'

#-------------------------------------

#Setting your username and password.

username = "USERNAME"

password = "PASSWORD"

#-------------------------------------


在这里设置。最近忙于学业今天晚上出来通宵才看到- -

TOP

回复 5# BAT-VBS

老大啊,这个咋用呢?要安装什么?如何把代码使用?

TOP

TOP

回复 8# BAT-VBS
Thank you so much!

TOP

回复 6# canyuexiaolang


Logon failed,meet the verification code or password error.

这个报错信息如何解决?

TOP

回复 3# 522235677


   - -  python 不是VBS ...

TOP

返回列表