尊敬的各位,今日我荣幸介绍一款卓越的Python迷你项目——QQ空间自动双击神器。听起来就颇具吸引力吧?确实,这不仅是一段基础脚本,还能实现自动cookie更新,免去了繁琐的手动操作。此外,我集成了滑动验证码破解功能,成功率恒达100%。您是否跃跃欲试?让我们一起深入探索。
1.项目背景:为什么要自动双击QQ空间?
探讨开展自动双击QQ空间项目的必要性。您是否认同频繁手动输入密码与cookie的繁琐?尤其是在频繁访问特定页面的情况下,此类操作无疑令人不胜其烦。因此,我选择运用Python开发一项解决方案,旨在自动化完成这些繁琐任务。使用该方法后,您便能将宝贵时间投入更有趣的活动中,而非陷入机械重复的过程中。
此外,该项目的独特之处在于,它每6小时自动刷新cookie。因此,用户无需担忧cookie过期,系统将自动处理。此举是否让您仿佛顿悟成为技术高手?
2.环境配置:准备工作要做好
启动编码流程前,需验证环境设置准确无误。此环节至关重要,环境配置的优劣直接关联程序执行成效。因此,须先行安装若干关键库,例如selenium、requests等,以增强对浏览器的操控及模拟用户行为能力。
此外,务必确保您的Python运行环境已妥善设置。对于初学者,安装与配置Python可能需投入一定时间学习。尽管如此,不必忧虑,网络资源丰富,众多教程可供参照,耐心学习,您定能顺利完成。
3.代码实现:一步步教你写自动双击脚本
完成筹备工作后,即将踏入编写关键代码的阶段。首要任务是创建一个模拟QQ空间用户登录的脚本。该脚本将自动启动浏览器,输入指定QQ号及密码,并执行登录动作。看似简易,但其中涉及众多考量,例如处理密码输入的回显及保障登录成功率等细节问题。
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()
本系统旨在实现cookies自动刷新功能。其核心机制为,每6小时自动抓取并储存在本地新的cookies。以此确保,即便cookies失效,用户依然能顺畅登录QQ空间。该功能的实现简单,仅需在代码中嵌入一个计时器,定时每6小时刷新一次。
4.滑动验证码破解:挑战与解决方案
在探讨QQ空间的自动登录机制时,滑动验证码的干扰尤为显著。这一环节需手动操作,造成极大不便,甚至导致用户失望。因此,我决心尝试破解,寻求编程解决方案。经深入研究与试验,我成功发现了可行的解决途径。
该方案主要采用模拟用户滑动动作的机制,自动完成滑动式验证码的验证。流程涉及先抓取验证码图像,利用图像识别技术定位滑块,并随后模拟用户滑动动作至正确位置。尽管过程看似繁杂,实际执行却颇为简便,只要掌握基础图像处理和模拟操作技术,即可轻松实施。
5.本地测试:确保一切正常
软件开发完成后,随即进入测试阶段。初步在本地实施测试验证程序稳定性,过程中识别出若干小故障,如程序偶发停滞或滑动验证码识别不稳固。经调试与优化,上述问题均已妥善解决。
本地测试验证成功后,我选择将应用程序部署至腾讯云服务器,以观察其在异地网络环境中的运行状况。测试结果显示,程序运行稳定,滑动验证码破解成功率仍维持在100%,此举进一步增强了我对自身技术能力的信心。
6.服务器部署:让程序24小时不间断运行
为确保程序昼夜不息运行,我选择将其部署于腾讯云服务器。如此,即便远离电脑,程序亦能自主登录QQ空间,更新cookie,攻克滑动验证码。这不仅省时,更令我仿佛具有无敌黑客之力。
在部署过程中,遭遇了若干技术障碍,如服务器与本地网络配置不一致,引起登录环节的响应延迟。幸运的是,这些挑战可通过代码的细微调整轻松克服,确保程序在服务器端稳定运行。
7.总结与展望:未来还有更多可能
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()
本分享内容至此完毕。该项目让我不仅掌握了利用Python实现QQ空间自动双击的方法,也对图像处理与模拟交互技巧有了深入理解。更为重要的是,我深刻感受到了编程带来的乐趣与成就。
展望未来,我将继续深入挖掘自动化领域的多个项目,例如消息自动应答、批量点赞功能等。如有对该项目的好奇或疑问,请随时在评论区留言。我将竭诚解答您的困惑,并与您分享更丰富的技术见解。
您是否考虑利用Python进行自动化任务的开发?若有所想,您期望实现哪方面的自动化?在评论区分享您的构思,让我们共同交流与提升。