本文實例為大家分享了python實現qq空間自動點贊的具體代碼,供大家參考,具體內容如下
項目github地址
使用python實現qq空間自動點贊功能。
需自行安裝庫并配置環境。
我想實現的是每6個小時就自動更新一次cookie。這也是和網上其他版本相比具有的優點。不用手動輸入cookie。更加自動。(不負責任的說,這個功能沒有測試過。)
程序運行方法:將代碼存為.py文件,運行即可。
輸入qq密碼的時候采用了linux登錄的方式——沒有回顯。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
from selenium import webdriver from selenium.webdriver.chrome.options import options import time import requests import demjson import re import datetime import getpass qq = '' pwd = '' def print_time(): print (datetime.datetime.now(), end = ' ' ) def get_gtk(p_skey): hash = 5381 for i in p_skey: hash + = ( hash << 5 ) + ord (i) print_time() print ( '生成gtk' ) return hash & 0x7fffffff def change_cookie(cookie): s = '' for c in cookie: s = s + c[ 'name' ] + '=' + c[ 'value' ] + '; ' return s def check_time(): now = datetime.datetime.now() hour = str (now)[ 11 : 13 ] minute = str (now)[ 14 : 16 ] second = str (now)[ 17 : 19 ] if 0 = = int (hour) % 6 and minute = = '00' and int (second) < 30 : return true else : return false def get_cookie(): chrome_options = options() chrome_options.add_argument( '--headless' ) driver = webdriver.chrome(chrome_options = chrome_options) driver.get( 'https://qzone.qq.com/' ) driver.switch_to.frame( 'login_frame' ) driver.find_element_by_id( 'switcher_plogin' ).click() driver.find_element_by_id( 'u' ).clear() driver.find_element_by_id( 'u' ).send_keys(qq) driver.find_element_by_id( 'p' ).clear() driver.find_element_by_id( 'p' ).send_keys(pwd) driver.find_element_by_id( 'login_button' ).click() time.sleep( 1 ) driver.find_element_by_id( 'qz_body' ).click() cookie = driver.get_cookies() # print(cookie) driver.close() driver.quit() print_time() print ( '提取cookie' ) return cookie def get_args(): cookie = get_cookie() for c in cookie: if c[ 'name' ] = = 'p_skey' : p_skey = c[ 'value' ] break cookie = change_cookie(cookie) # print(p_skey) gtk = get_gtk(p_skey) return cookie, gtk def do_like(d, gtk, headers): url = 'https://user.qzone.qq.com/proxy/domain/w.qzone.qq.com/cgi-bin/likes/internal_dolike_app?g_tk=' + str (gtk) body = { 'qzreferrer' : 'http://user.qzone.qq.com/' + qq, 'opuin' : qq, 'from' : 1 , 'active' : 0 , 'fupdate' : 1 } try : html = d[ 'html' ] # print(html) # unikey = re.search(r'data-unikey=\"http:[^"]*\"', html).group(0) # curkey = re.search(r'data-curkey=\"http:[^"]*\"', html).group(0) # print(unikey, curkey) temp = re.search( 'data-unikey="(http[^"]*)"[^d]*data-curkey="([^"]*)"[^d]*data-clicklog=("like")[^h]*href="javascript:;" rel="external nofollow" rel="external nofollow" ' , html); if temp is none: return unikey = temp.group( 1 ); curkey = temp.group( 2 ); # print(unikey, curkey) body[ 'unikey' ] = unikey body[ 'curkey' ] = curkey body[ 'appid' ] = d[ 'appid' ] body[ 'typeid' ] = d[ 'typeid' ] body[ 'fid' ] = d[ 'key' ] r = requests.post(url, data = body, headers = headers) if 200 = = r.status_code: print_time() print ( '給 ' + d[ 'nickname' ] + ' 點贊' ) except : return def get_content(headers, gtk): try : r = requests.get( 'http://ic2.s8.qzone.qq.com/cgi-bin/feeds/feeds3_html_more?uin=0924761163&scope=0&view=1&daylist=&uinlist=&gid=&flag=1&filter=all&applist=all&refresh=0&aisortendtime=0&aisortoffset=0&getaisort=0&aisortbegintime=0&pagenum=1&externparam=offset%3d6%26total%3d97%26basetime%3d1470323193%26feedsource%3d0&firstgetgroup=0&icservertime=0&mixnocache=0&scene=0&begintime=0&count=10&dayspac=0&sidomain=cnc.qzonestyle.gtimg.cn&useutf8=1&outputhtmlfeed=1&getob=1&g_tk=' + str (gtk), headers = headers) r = r.text[ 10 : - 2 ] r = demjson.decode(r) data = r[ 'data' ][ 'data' ] print_time() print ( '獲取了 ' + str ( len (data)) + ' 條說說' ) return data except : return [] def main(): print_time() print ( '程序運行...' ) global qq global pwd qq = input ( 'qq:' ) pwd = getpass.getpass( 'password:' ) headers = { 'user-agent' : 'mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/69.0.3497.100 safari/537.36' } cookie, gtk = get_args() headers[ 'cookie' ] = cookie while true: time.sleep( 1 ) if check_time(): cookie, gtk = get_args() headers[ 'cookie' ] = cookie print_time() print ( '更新了 cookie 和 gtk' ) data = get_content(headers, gtk) for d in data: do_like(d, gtk, headers) if __name__ = = '__main__' : main() |
這個程序在本地跑沒有問題,但是我希望它能在我的騰訊云服務器上一直運行。
我在遼寧,服務器在北京,導致登錄qq空間時會有滑動驗證碼。
于是我按照網上的教程,結合qq空間滑動驗證碼的實際情況,實現了qq空間滑動驗證碼的破解。
值得一提的是,目前成功率是100%。
有的時候不能完全重合,但還是會成功。
具體思路我就不貼出來了,感興趣的朋友可以私信我。
下面是整合了破解滑動驗證碼部分的代碼。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
from selenium import webdriver from selenium.webdriver.chrome.options import options from selenium.webdriver.common.action_chains import actionchains from pil import image from io import bytesio import time import requests import demjson import re import datetime import getpass qq = '' pwd = '' def print_time(): print (datetime.datetime.now(), end = ' ' ) def get_gtk(p_skey): hash = 5381 for i in p_skey: hash + = ( hash << 5 ) + ord (i) print_time() print ( '生成gtk' ) return hash & 0x7fffffff def change_cookie(cookie): s = '' for c in cookie: s = s + c[ 'name' ] + '=' + c[ 'value' ] + '; ' return s def check_time(): now = datetime.datetime.now() hour = str (now)[ 11 : 13 ] minute = str (now)[ 14 : 16 ] second = str (now)[ 17 : 19 ] if 0 = = int (hour) % 6 and minute = = '00' and int (second) < 30 : return true else : return false def get_image_difference(back_img, full_img): width, height = full_img.size for w in range ( 0 , width): for h in range ( 0 , height): back_pixel = back_img.getpixel((w, h)) full_pixel = full_img.getpixel((w, h)) if back_pixel ! = full_pixel and w > 340 and h > 10 and abs (back_pixel[ 0 ] - full_pixel[ 0 ])> 50 and abs (back_pixel[ 1 ] - full_pixel[ 1 ])> 50 and abs (back_pixel[ 2 ] - full_pixel[ 2 ])> 50 : return true, w return false, - 1 def get_cookie(): chrome_options = options() chrome_options.add_argument( '--headless' ) driver = webdriver.chrome(chrome_options = chrome_options) driver.get( 'https://qzone.qq.com/' ) driver.switch_to.frame( 'login_frame' ) driver.find_element_by_id( 'switcher_plogin' ).click() driver.find_element_by_id( 'u' ).clear() driver.find_element_by_id( 'u' ).send_keys(qq) driver.find_element_by_id( 'p' ).clear() driver.find_element_by_id( 'p' ).send_keys(pwd) driver.find_element_by_id( 'login_button' ).click() time.sleep( 3 ) frame = driver.find_element_by_xpath( '//*[@id="newvcodeiframe"]/iframe' ) driver.switch_to.frame(frame) # back_url = driver.find_element_by_id( 'slidebkg' ).get_attribute( 'src' ) full_url = back_url.replace( 'hycdn_1' , 'hycdn_0' ) r = requests.get(back_url) file = bytesio(r.content) back_img = image. open ( file ) r.status_code = 500 while 200 ! = r.status_code: r = requests.get(full_url) file = bytesio(r.content) full_img = image. open ( file ) r, w = get_image_difference(back_img, full_img) if r is false: return # print(w) # 280 * 158 # 680 * 390 # 55 * 55 # 136 * 136 # 214 slide = driver.find_element_by_id( 'tcaptcha_drag_thumb' ) actionchains(driver).click_and_hold(slide).perform() actionchains(driver).move_by_offset(xoffset = w / 680 * 250 , yoffset = 0 ).perform() actionchains(driver).release(slide).perform() # print(back_img.size) # print(cut_img.size) # print(full_img.size) time.sleep( 2 ) driver.find_element_by_id( 'qz_body' ).click() cookie = driver.get_cookies() # print(cookie) driver.close() driver.quit() print_time() print ( '提取cookie' ) return cookie def get_args(): cookie = get_cookie() for c in cookie: if c[ 'name' ] = = 'p_skey' : p_skey = c[ 'value' ] break cookie = change_cookie(cookie) # print(p_skey) gtk = get_gtk(p_skey) return cookie, gtk def do_like(d, gtk, headers): url = 'https://user.qzone.qq.com/proxy/domain/w.qzone.qq.com/cgi-bin/likes/internal_dolike_app?g_tk=' + str (gtk) body = { 'qzreferrer' : 'http://user.qzone.qq.com/' + qq, 'opuin' : qq, 'from' : 1 , 'active' : 0 , 'fupdate' : 1 } try : html = d[ 'html' ] # print(html) # unikey = re.search(r'data-unikey=\"http:[^"]*\"', html).group(0) # curkey = re.search(r'data-curkey=\"http:[^"]*\"', html).group(0) # print(unikey, curkey) temp = re.search( 'data-unikey="(http[^"]*)"[^d]*data-curkey="([^"]*)"[^d]*data-clicklog=("like")[^h]*href="javascript:;" rel="external nofollow" rel="external nofollow" ' , html); if temp is none: return unikey = temp.group( 1 ); curkey = temp.group( 2 ); # print(unikey, curkey) body[ 'unikey' ] = unikey body[ 'curkey' ] = curkey body[ 'appid' ] = d[ 'appid' ] body[ 'typeid' ] = d[ 'typeid' ] body[ 'fid' ] = d[ 'key' ] r = requests.post(url, data = body, headers = headers) if 200 = = r.status_code: print_time() print ( '給 ' + d[ 'nickname' ] + ' 點贊' ) except : return def get_content(headers, gtk): try : r = requests.get( 'http://ic2.s8.qzone.qq.com/cgi-bin/feeds/feeds3_html_more?uin=0924761163&scope=0&view=1&daylist=&uinlist=&gid=&flag=1&filter=all&applist=all&refresh=0&aisortendtime=0&aisortoffset=0&getaisort=0&aisortbegintime=0&pagenum=1&externparam=offset%3d6%26total%3d97%26basetime%3d1470323193%26feedsource%3d0&firstgetgroup=0&icservertime=0&mixnocache=0&scene=0&begintime=0&count=10&dayspac=0&sidomain=cnc.qzonestyle.gtimg.cn&useutf8=1&outputhtmlfeed=1&getob=1&g_tk=' + str (gtk), headers = headers) r = r.text[ 10 : - 2 ] r = demjson.decode(r) data = r[ 'data' ][ 'data' ] print_time() print ( '獲取了 ' + str ( len (data)) + ' 條說說' ) return data except : return [] def main(): print_time() print ( '程序運行...' ) global qq global pwd qq = input ( 'qq:' ) pwd = getpass.getpass( 'password:' ) headers = { 'user-agent' : 'mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/69.0.3497.100 safari/537.36' } cookie, gtk = get_args() headers[ 'cookie' ] = cookie while true: time.sleep( 1 ) if check_time(): cookie, gtk = get_args() headers[ 'cookie' ] = cookie print_time() print ( '更新了 cookie 和 gtk' ) data = get_content(headers, gtk) for d in data: do_like(d, gtk, headers) if __name__ = = '__main__' : main() |
上面兩份代碼整體思路沒問題,但是偶爾會有一些小bug。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/qq_32862515/article/details/82977825