Fork me on GitHub Fork me on Gitee

python版 百度签到

经常玩贴吧,刚学python ,所以自己弄了一个python版的签到程序。自己的东西总是最好的。

登陆模块参考的http://www.crifan.com/emulate_login_website_using_python/。签到模块自己找百度瞎糊弄的,写的很烂,代码一点都不规范。有什么建议可以提

 

  1 # 594595116@qq.com
  2 
  3 import re
  4 import urllib
  5 import urllib2
  6 import cookielib
  7 import json
  8 import sys
  9 import time
 10 from urllib import quote,unquote
 11 from urllib2 import HTTPError
 12 from urllib2 import URLError
 13 
 14 def t():
 15     print "按(Y/y)继续,其他退出!"
 16     t = raw_input(">>")
 17     if (t=="y" or t=="Y"):
 18         Login()
 19     else :
 20         print "感谢您的使用。"
 21         print "email:594595116@qq.com"
 22         time.sleep(5)  
 23 
 24 
 25 #登陆模块
 26 def Login():
 27     
 28     ID = raw_input("百度账号(暂不支持手机和邮箱登陆) :")
 29     ID1 = ID.decode("gb18030").encode("utf-8") #--注意编码,可能网站会识别不了而导致登陆失败
 30     PassWord = raw_input("密码 :")    
 31     
 32     try :
 33         cj = cookielib.CookieJar();
 34         opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj));
 35         urllib2.install_opener(opener); 
 36         
 37         #打开百度获得 cookie
 38         baidu_URL = "http://www.baidu.com"
 39         staticpage = "http://www.baidu.com/cache/user/html/jump.html";
 40         baidu_Login_URL = "https://passport.baidu.com/v2/api/?login";     
 41         baidu_OPEN = urllib2.urlopen(baidu_URL)
 42         
 43         #获得所需的数据 token
 44         getapiUrl = "https://passport.baidu.com/v2/api/?getapi&class=login&tpl=mn&tangram=true";
 45         getapiResp = urllib2.urlopen(getapiUrl);
 46         getapiRespHtml = getapiResp.read();
 47         
 48         foundTokenVal = re.search("bdPass\.api\.params\.login_token='(?P<tokenVal>\w+)';", getapiRespHtml);    
 49         tokenVal = foundTokenVal.group("tokenVal");
 50         
 51         #生成要发送的数据
 52         if(foundTokenVal):
 53             LoginData = {
 54                 'staticpage':staticpage,#'http://www.baidu.com/cache/user/html/v3Jump.html',
 55                 'charset':'UTF-8',
 56                 'token':tokenVal,
 57                 'tpl':'mn',
 58                 #'apiver':'v3',
 59                 #'tt':'1375838455898',
 60                 #'codestring':'',
 61                 'isPhone':'false',
 62                 #'safeflg':'0',
 63                 #'u':'http://www.baidu.com/',
 64                 #'quick_user':'0',
 65                 'username':ID1,
 66                 'password':PassWord,
 67                 #'verifycode':'',
 68                 'mem_pass':'on',
 69                 #'ppui_logintime':'11211',
 70                 'callback':'parent.bdPass.api.login._postCallback',
 71                 }; 
 72             
 73             
 74             
 75             #把发送数据转换格式
 76             LoginData = urllib.urlencode(LoginData);    
 77             
 78             #生成请求数据
 79             baidu_Login_Request = urllib2.Request(baidu_Login_URL, LoginData);
 80             
 81             #添加数据头部
 82             baidu_Login_Request.add_header('Content-Type', "application/x-www-form-urlencoded");
 83             baidu_Login_Request.add_header('User-Agent', "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31");
 84            
 85             #发送请求,尝试登陆
 86             baidu_Login_Open = urllib2.urlopen(baidu_Login_Request);
 87             
 88             #验证登陆是否成功
 89             #tt = urllib2.urlopen(baidu_URL).read().encode("gb18030")
 90             #print tt
 91             #ttt = re.search(r'href="/p/([0-9A-Za-z%]*)\?from=super"',tt).grop(1)
 92             #print ttt
 93             yanzheng_url = "http://www.baidu.com/p/" + ID1
 94             yanzheng_open = urllib2.urlopen(yanzheng_url)
 95             yanzheng_open = yanzheng_open.read().encode("gb18030")
 96             yanzheng = re.search(r"'sexTitle' : '([^ ]*)'", yanzheng_open).group(1)
 97             
 98             if(yanzheng == ""):
 99                 print "%s  成功登陆, ^_^" %ID
100                 print "签到正在进行中。。"
101                 Sign(ID)
102             else:
103                 print "%s  登陆失败! 请稍后尝试! " %ID
104                 Login()
105         else:
106             print "%s  登陆失败! 请稍后尝试! " %ID
107             Login()
108                # print "Fail to extract token value from html=",getapiRespHtml;    
109             
110     except :
111         print "登陆出错啦!"
112         t()     
113         
114         
115 #签到模块      
116 def Sign(ID):
117     
118     try :
119         #签到请求地址
120         Sign_Request_Url = "http://tieba.baidu.com/sign/add"
121     
122         #用户的贴吧
123         ID_Tieba_Url = "http://www.baidu.com/p/" + ID + "?from=tieba" 
124         ID_Tieba_Open = urllib2.urlopen(ID_Tieba_Url).read()
125 
126         #i贴吧id
127         itieba_id = re.search(r'i\\/([0-9]*)\\/others\?from=princess', ID_Tieba_Open).group(1)
128         index = "http://tieba.baidu.com/i/" + itieba_id + "?fr=index"
129         
130         #匹配贴吧
131         index_open = urllib2.urlopen(index).read()
132         Tieba = re.findall(r'kw=([0-9A-Za-z%]*)&fr=itb_favo&fp=favo" target="_blank">([^ ]*)</a>', index_open)
133        
134         if Tieba != []:
135             for i in range(len(Tieba)):
136                 Tieba_Url = "http://tieba.baidu.com/f?kw=" +  Tieba[i][0]
137                 Tieba_Open = urllib2.urlopen(Tieba_Url).read()
138 
139                 #获取tbs
140                 Get_Tbs = re.search(r'PageData.tbs = "([0-9a-z]*)";',Tieba_Open).group(1)
141 
142                 #贴吧名字编码
143                 Tieba_Name = urllib.unquote(Tieba[i][0]).decode("gb18030").encode("utf-8")
144                 kw = Tieba_Name
145                    
146                 #签构造到请求数据 和 编码
147                 Sign_Request_Data={'ie':'utf-8','kw':kw,'tbs':Get_Tbs}
148                 Sign_Request_Data = urllib.urlencode(Sign_Request_Data)
149                    
150                 #发送签到请求
151                 Sign_Request = urllib2.Request(Sign_Request_Url, Sign_Request_Data)
152                 Sign_Request.add_header('User-agent', "Mozilla/5.0 (X11; Linux i686)")
153                 Sign = urllib2.urlopen(Sign_Request)
154                    
155                 #获取响应数据
156                 Response = Sign.read()
157                 
158                 #把响应转成字典
159                 Response = json.loads(Response)
160                 #print Response
161                    
162                 Tieba_Name = urllib.unquote(Tieba[i][0]).decode("gb18030").encode("utf-8")
163                 #print Tieba_Name
164                 #print unquote(Tieba[i][0])
165                 #print Tieba[i][0]
166                 if(Response['error']=="") :
167                     user_sign_rank = int(Response['data']['uinfo']['user_sign_rank'])                      #第几个签到
168                     cont_sign_num = int(Response['data']['uinfo']['cont_sign_num'])                        #连续签到
169                     cout_total_sing_num = Response['data']['uinfo']['cout_total_sing_num']                 #累计签到
170                     print "%d..%s             签到成功,第%d个签到,连续签到%d天,累计签到%d天" %(i, unquote(Tieba[i][0]), user_sign_rank, cont_sign_num, cout_total_sing_num)
171                 else :
172                     print "%d.%s" %(i,unquote(Tieba[i][0]))            # print "%d.%s %s" %(i,unquote(Tieba[i][0]),Response['error'])   不知道为什么这样是不行的,总是乱码。
173                     print "--------------------%s"  %Response['error'] # print "%d.%s %s" %(i,Tieba_Name,Response['error'])             这样却可以,所以把他们拆分了
174                 time.sleep(3)
175         else :
176             print "你还没有喜欢的贴吧!"
177                    
178         print "一共有%d个喜欢的吧" %(i+1)            
179         t()
180     except :
181         print "签到出错啦! "
182         t()
183 
184 
185 if __name__ == "__main__" :
186     Login()
187     

 

 

 

经验总结:1,编码问题需要注重 2,编码要规范

 

 

 

 

 

 

 

posted @ 2013-08-08 18:37  伴途の永远  阅读(1623)  评论(7编辑  收藏  举报