本文主要是介绍百度贴吧自动签到器,python,,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
写了一个百度贴吧自动签到器,实现的功能:
1.输入帐号和密码,自动签到所有关注的吧。
2.验证是否登录成功,用了re模块筛选。
3.加入了保存用户数据的功能,在脚本所在目录创建txt文档,保存帐号。下次可以直接选择帐号。
这是帐号保存功能的脚本,现学了pickle模块:
# -*- coding:utf8 -*-
# 2014.1.28 14:13 为贴吧签到0.1编写保存帐号信息的脚本
import os
import pickle
def account():
#检测txt是否存在,不存在则创建包含空字典的txt
if not os.path.exists('config.txt'):
print u'初始化用户配置'
account = {}
f1 = open('config.txt','wb')
pickle.dump(account,f1)
f1.close()
# load 字典
f2 = open('config.txt','rb')
account = pickle.load(f2)
f2.close()
if account !={}:
print u'以下是已保存的用户帐号:'
for key in account.keys():
print key.decode('gbk')
print u'请输入数字来选择对应的帐号。如果您要填写新的帐号,直接按回车键'
num = raw_input('>')
##############################可以是原来帐号,也可以是现在的帐号
try:
id = account.items()[int(num)-1]
#print id 这是什么编码?('\xb0\xd9\xca\xc2\xb2\xbb\xca\xc7\xb6\xab\xce\xf7', '*********')
(name,pwd)=id
except:
name = raw_input(u'请输入用户名,按回车键结束:'.encode('gbk'))
pwd = raw_input(u'请输入密码:,按回车键结束:'.encode('gbk'))
#写入字典
account[name] = pwd
f3 = open('config.txt','wb')
pickle.dump(account,f3)
f3.close()
else:
name = raw_input(u'请输入用户名,按回车键结束:'.encode('gbk'))
pwd = raw_input(u'请输入密码:,按回车键结束:'.encode('gbk'))
#写入字典
account[name] = pwd
f3 = open('config.txt','wb')
pickle.dump(account,f3)
f3.close()
print name,pwd
return (name,pwd)
这是主脚本:
# -*- coding:utf8 -*-
# 2014.1.28 0:05 家里,坚持,便会柳暗花明。比别人笨无所谓,那就多花一点时间撒
import urllib, urllib2, cookielib, re, time
import account
from bs4 import BeautifulSoup
baidu_url = 'http://www.baidu.com/' #为获取cookie,具体不懂
token_url = 'https://passport.baidu.com/v2/api/?getapi&tpl=pp&apiver=v3&class=login' #此链接可获取token,网址怎么分析出来的,不理解
login_url = 'https://passport.baidu.com/v2/api/?login' #post网址,1E F12分析
like_url = 'http://tieba.baidu.com/f/like/mylike?&pn=' #怎么分析出来的
sign_url = 'http://tieba.baidu.com/sign/add' #post
# 自动保存cookie 整个程序内都有效还是?
def welcome_():
print '-'*40
print u'--你好,我是百度贴吧签到器!~'
print u'--使用此签到器需要录入您的帐号,请确保周围无人'
print u'--我绝不会记录你的帐号信息。并且我目前也不会...'
print u'--360会误报有毒,我也不知道为什么。反正360巨坑爹!'
print u'--作者:百事不是东西,时间:2014.1.28,Version:0.1'
print u'\n'
def cookie_():
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
urllib2.urlopen(baidu_url)
def token_():
content = urllib2.urlopen(token_url).read()
token = re.findall('"token" : "(.*?)"',content)[0]
#print token
return token
def login_(name,pwd):
while True:
"""
name = raw_input(u'请输入用户名,按回车键结束:'.encode('gbk'))
pwd = raw_input(u'请输入密码:,按回车键结束:'.encode('gbk'))
"""
# 必要的表单数据,是慢慢筛出来的-->
postdata = {
'staticpage':'https://passport.baidu.com/static/passpc-account/html/v3Jump.html',
#'charset':'UTF-8',
'token':token,
'tpl':'pp',
'isPhone':'false',
'username':name,
'password':pwd,
#'callback':'parent.bd__pcbs__poaxot',
#'u':'https://passport.baidu.com/'
}
postdata = urllib.urlencode(postdata)
req = urllib2.Request(login_url,postdata)
sendpost = urllib2.urlopen(req)
center = urllib2.urlopen('http://passport.baidu.com/center').read()
if re.findall('百度帐号登录',center)==[]:
print u'登录成功'+'-'*20
break
else:
s = u'登录失败,请检查用户名和密码'
width = (80-len(s)*2)/2
print ' '*width+s
time.sleep(1)
(name,pwd) = account.account()
##############################################加入登录是否成功的测试
# 测试是否登录成功
#content = urllib2.urlopen('http://passport.baidu.com/center').read()
#open('1.txt','wb').write(content)
def sign_():
print u'开始签到'+'-'*20
i = 1
while True:
like_content = urllib2.urlopen(like_url+str(i)).read()
soup =BeautifulSoup(like_content)
for a in soup.find_all('a',href=True,title=True,class_=False):
name = a.string # 貌似BS的内容全为unicode
href = a.get('href')
print name+u'吧,签到成功'
tieba_url = 'http://tieba.baidu.com'+href
tbs = re.findall('PageData.tbs = "(.*?)"',urllib2.urlopen(tieba_url).read())[0]
#print tbs
postdata = {
'ie':'utf-8',
'kw':name.encode('utf8'), # 因为BS的内容为unicode
'tbs':tbs
}
postdata = urllib.urlencode(postdata)
req = urllib2.Request(sign_url,postdata)
urllib2.urlopen(req)
print '-'*40
#print content
if re.findall('',like_content) == []:
print u'全部贴吧签到完毕,输入任意键退出程序'
a = raw_input('>-')
break
i+=1
welcome_()
cookie_()
token = token_()
(name,pwd)=account.account()
login_(name,pwd)
sign_()
这篇关于百度贴吧自动签到器,python,的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!