-
python版 百度签到
添加时间:2013-8-8 点击量:经常玩贴吧,刚学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 urllib import quote,unquote
11 urllib2 import HTTPError
12 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(rhref=/p/([0-9A-Za-z%])\?=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(rsexTitle : ([^ ]), 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 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 + ?=tieba
124 ID_Tieba_Open = urllib2.urlopen(ID_Tieba_Url).read()
125
126 #i贴吧id
127 itieba_id = re.search(ri\\/([0-9])\\/others\?=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(rkw=([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(rPageData.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,编码要规范
无论对感情还是对生活,“只要甜不要苦”都是任性而孩子气的,因为我们也不完美,我们也会伤害人。正因为我们都不完美,也因为生活从不是事事如意,所以对这些“瑕疵”的收纳才让我们对生活、对他人的爱变得日益真实而具体。—— 汪冰《世界再亏欠你,也要敢于拥抱幸福》