本帖最后由 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跨平台感觉真爽...
代码如下:- # -*- coding:utf-8 -*-
- #-----------------------------------------------
- #signBaidu 2.1 by Ricter 2013-06-14
- #E-mail:canyuexiaolang@gmail.com
- #Website:http://ricter.rp2.jybox.net
- #-----------------------------------------------
- import re,time,os
- import urllib,urllib2
- import threading,sys
-
- def CheckUpdate():
- checkupdate = signBaidu()
- version = 'v2.1'
- checkupdate.getURL('http://ricter.rp2.jybox.net/download/python/signBaidu/update.txt', None)
- if checkupdate.requestData.strip('\n').split(',')[0] == version:
- print 'signBaidu is already the newest version.'
- main(isthread)
- elif checkupdate.requestData.strip('\n').split(',')[1] == 'important':
- 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])
- else:
- print 'signBaidu %s is not the newest version(%s).' % (version, checkupdate.requestData.strip('\n').split(',')[0])
- main(isthread)
-
- class signBaidu(object):
- mainUrl = 'http://wapp.baidu.com'
- loginUrl = 'http://wappass.baidu.com/passport/'
- username = ''
- password = ''
- vcodestr = ''
- vcode = ''
- tbUrl = ''
- result = ''
- barlist = []
- faillist = []
- uid = ''
- postDict = {
- 'username' : '',
- 'password' : '',
- 'submit' : '登录',
- 'isphone' : '0',
- 'vcodestr' : '',
- 'u' : 'http%3A%2F%2Fwap.baidu.com%3Fuid%3D',
- 'tpl' : '',
- 'ssid' : '',
- 'from' : '',
- 'uid' : '',
- 'pu' : '',
- 'tn' : '',
- 'bdcm' : '',
- 'type' : '',
- 'bd_page_type': ''
- }
- requestData = ''
-
- def showusrandpwd(self):
- """Show Username and Password"""
- #print 'in showusrandpwd'
- print '-' * 40
- print 'Username:',self.username, '\nPassword:',self.password
- print '-' * 40
-
- def getURL(self,url,data):
- """Open webpages and get the HTML code"""
- #print 'in getURL'
- req = urllib2.Request(url, data)
- req.add_header('Content-Type', 'application/x-www-form-urlencoded')
- resp = urllib2.urlopen(req)
- #print 'Get Url:', url
- self.requestData = resp.read()
-
- def getLoginUid(self):
- """Get Login Uid"""
- #print 'in getLoginUid'
- resp = urllib2.urlopen(self.loginUrl)
- uid = re.search(r'[_0-9]{17}', resp.read())
- if uid:
- self.uid = uid.group()
- print 'Get uid success:', self.uid
- else:
- print 'Get uid fail.'
-
- def checkIsUidGet(self):
- """Check self.uid is defineded or not"""
- #print 'in checkIsUidGet'
- if self.uid:
- return True
- else:
- return False
-
- def loginBaidu(self):
- """Login Baidu bar and get infomation"""
- #print 'in loginBaidu'
- self.getLoginUid()
- if self.checkIsUidGet():
- self.postDict['username'] = self.username
- self.postDict['password'] = self.password
- self.postDict['uid'] = self.uid
- self.postDict['u'] = self.postDict['u'] + self.uid
- #print self.postDict
- postData = urllib.urlencode(self.postDict)
- self.getURL(self.loginUrl, postData)
- #print self.requestData
- self.result = self.getLoginRequest()
- if self.result == '0':
- postData = urllib.urlencode(self.postDict)
- self.getURL(self.loginUrl, postData)
- self.result = self.getLoginRequest()
- elif self.result == '1':
- print 'Login FAIL!'
- else:
- print 'Construct Post data fail.'
-
- def getLoginRequest(self):
- """Get infomation from login page"""
- #print 'in getLoginRequest'
- if "请您输入验证码" in self.requestData:
- for i in range(0,len(self.requestData.split('\n'))):
- if '验证码:' in self.requestData.split('\n')[i]:
- vcodeimgUrl = self.requestData.split('\n')[i].split('<')[2].split('=')[1].split(' ')[0].strip('"')
- self.vcodestr = vcodeimgUrl.split('?')[1]
- print '-' * 79
- print vcodeimgUrl
- print '-' * 79
- self.vcode = raw_input('Verification code:')
- self.postDict['vcodestr'] = self.vcodestr
- self.postDict['verifycode'] = self.vcode
- #print self.postDict
- return '0'
-
- elif "您输入的密码有误" in self.requestData:
- print 'Password WRONG!'
- #print self.requestData
- return '1'
-
- elif "您输入的验证码有误" in self.requestData:
- print 'Verifycode WRONG!'
- #print self.requestData
- return '1'
-
- elif "系统错误" in self.requestData:
- print 'Unexpect Error!'
- #print self.requestData
- return '1'
-
- else:
- for line in self.requestData.split('<'):
- if 'itj=23' in line:
- self.tbUrl = line.split('"')[1].replace('amp;', '')
- print 'Get Baidu Bar Url...Success!'
- #print self.requestData
-
- def getBar(self):
- """Get Baidu Bar List"""
- #print 'in getBar'
- favoUrl = ''
- self.getURL(self.tbUrl, None)
- #print self.requestData
- for line in self.requestData.split('<'):
- if 'tab=favorite' in line:
- favoUrl = self.mainUrl + line.split('"')[1]
- break
-
- if favoUrl:
- self.getURL(favoUrl, None)
- #print self.requestData
- for line in self.requestData.split('<'):
- if 'm?kw' in line:
- tmp_tbUrl = favoUrl.split('?')[0] + "?" + line.split('"')[1].split('?')[1]
- self.barlist.append(tmp_tbUrl.replace('amp;', ''))
- #print tmp_tbUrl.replace('amp;', '')
-
- def signBar(self):
- """Sign in Baidu Bar"""
- #print 'in signBar'
- if self.checkBar():
- print 'OK!'
- print '-' * 40
- print 'Baidu Bar Sign starting...'
- for url in self.barlist:
- self.getURL(url, None)
- for line in self.requestData.split('<'):
- if 'sign?' in line:
- self.getURL(self.mainUrl + line.split('"')[1].replace('amp;',''),None)
- break
- for i in range(len(self.requestData.split('/'))):
- if 'name="top"' in self.requestData.split('/')[i]:
- temp_info = self.requestData.split('/')[i + 1]
- break
- if '签到成功' in temp_info:
- print 'Success:',urllib.unquote(url.split('=')[1]).decode('utf8'),\
- temp_info.split('>')[2].split('<')[0].decode('utf8') +\
- temp_info.split('>')[3].strip('<').decode('utf8')
- else:
- print 'Fail:',urllib.unquote(url.split('=')[1]).decode('utf8')
- self.faillist.append(url)
- print '-' * 40
- else:
- print 'Fail:No favourite bar.'
-
- def showBarName(self, bar_list):
- """Show Bar Name"""
- #print 'in showBarName'
- print '-' * 40
- for url in bar_list:
- print urllib.unquote(url.split('=')[1]).decode('utf8')
- print '-' * 40
-
- def checkBar(self):
- """Check Bar list"""
- #print 'in checkBar'
- if self.barlist:
- return True
- else:
- return False
-
- #--------------------------------------------------------------------------------
- def getURL_threading(self, url, data):
- """Open webpages and get the HTML code by each threading"""
- #print 'in getURL'
- req = urllib2.Request(url, data)
- req.add_header('Content-Type', 'application/x-www-form-urlencoded')
- resp = urllib2.urlopen(req)
- #print 'Get Url:', url
- return resp.read()
-
- def signBar_threading(self, barUrl):
- """Sign in Baidu Bar by using threading"""
- resp = self.getURL_threading(barUrl, None)
- for line in resp.split('<'):
- if 'sign?' in line:
- #print self.mainUrl + line.split('"')[1].replace('amp;','')
- resp = self.getURL_threading(self.mainUrl + line.split('"')[1].replace('amp;',''),None)
- break
-
- for i in range(len(resp.split('/'))):
- if 'name="top"' in resp.split('/')[i]:
- temp_info = resp.split('/')[i + 1]
- break
- if '签到成功' in temp_info:
- print 'Success:',urllib.unquote(barUrl.split('=')[1]).decode('utf8'),\
- temp_info.split('>')[2].split('<')[0].decode('utf8') +\
- temp_info.split('>')[3].strip('<').decode('utf8')
- else:
- print 'Fail:', urllib.unquote(barUrl.split('=')[1]).decode('utf8')
- self.faillist.append(barUrl)
-
- def sign_threading(self):
- worker = []
- names = locals()
- print 'OK!'
- print '-' * 40
- print 'Baidu Bar Sign starting...'
- for i in range(0,len(self.barlist)):
- names['t%d' % i] = threading.Thread(target=self.signBar_threading,args=(self.barlist[i],))
- names['t%d' % i].start()
- worker.append(names['t%d' % i])
- if i % 5 == 0 and i > 0:
- print 'Thread-' + str(i / 5) + ' Starting...'
- for row in worker:
- row.join()
- worker = []
- #--------------------------------------------------------------------------------
-
- def main(isthread):
- login = signBaidu()
- login.showusrandpwd()
- login.loginBaidu()
- if login.tbUrl:
- print 'Successful login!'
- print 'Get Baidu Bar List...',
- login.getBar()
- if login.barlist:
- if isthread:
- login.sign_threading()
- else:
- login.signBar()
- if login.faillist:
- print 'The following Baidu Bar sign fails, re-sign start...'
- login.barlist = login.faillist
- login.sign_threading()
-
-
-
- try:
- isthread = sys.argv[3]
- except:
- isthread = ''
-
- try:
- signBaidu.username = sys.argv[1]
- signBaidu.password = sys.argv[2]
- except:
- print '\nsignBaidu v2.1 by Ricter\n'
- print 'Python signBaidu.py username password [thread]\n'
- print '-'*67
- print ' Linux:[ricter@Ricter ~]# python signBaidu.py ricter 123456 thread'
- print ' Windows:C:\Users\Ricter\>python signBaidu.py batman 654321'
- print '-'*67
- else:
- CheckUpdate()
- #main(isthread)
复制代码
教程:http://blog.ricter.rp2.jybox.net/article/2013-04-03-21-00
来源:http://ricter.rp2.jybox.net/ |