python实现QQ空间自动点赞功能


Posted in Python onApril 09, 2019

本文实例为大家分享了python实现QQ空间自动点赞的具体代码,供大家参考,具体内容如下

项目github地址

使用python实现qq空间自动点赞功能。

需自行安装库并配置环境。

我想实现的是每6个小时就自动更新一次cookie。这也是和网上其他版本相比具有的优点。不用手动输入cookie。更加自动。(不负责任的说,这个功能没有测试过。)

程序运行方法:将代码存为.py文件,运行即可。

输入QQ密码的时候采用了linux登录的方式——没有回显。

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%。

有的时候不能完全重合,但还是会成功。

具体思路我就不贴出来了,感兴趣的朋友可以私信我。

下面是整合了破解滑动验证码部分的代码。

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。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中的连接符(+、+=)示例详解
Jan 13 Python
python安装教程
Feb 28 Python
PyCharm代码格式调整方法
May 23 Python
详解Python的数据库操作(pymysql)
Apr 04 Python
python多线程共享变量的使用和效率方法
Jul 16 Python
Python中最好用的命令行参数解析工具(argparse)
Aug 23 Python
在 Linux/Mac 下为Python函数添加超时时间的方法
Feb 20 Python
Tensorflow tf.nn.depthwise_conv2d如何实现深度卷积的
Apr 20 Python
Python实现爬取网页中动态加载的数据
Aug 17 Python
Python生成并下载文件后端代码实例
Aug 31 Python
Python 在 VSCode 中使用 IPython Kernel 的方法详解
Sep 05 Python
python 自动识别并连接串口的实现
Jan 19 Python
Python实现的多进程拷贝文件并显示百分比功能示例
Apr 09 #Python
Python使用crontab模块设置和清除定时任务操作详解
Apr 09 #Python
Python实现的读取文件内容并写入其他文件操作示例
Apr 09 #Python
Python实现根据日期获取当天凌晨时间戳的方法示例
Apr 09 #Python
Python匿名函数及应用示例
Apr 09 #Python
用Python中的turtle模块画图两只小羊方法
Apr 09 #Python
python3实现表白神器
Apr 09 #Python
You might like
php定义数组和使用示例(php数组的定义方法)
2014/03/29 PHP
PHP 实现类似js中alert() 提示框
2015/03/18 PHP
PHP模板解析类实例
2015/07/09 PHP
php实现微信公众号主动推送消息
2015/12/31 PHP
php_pdo 预处理语句详解
2016/11/21 PHP
php面向对象重点知识分享
2019/09/27 PHP
js 时间格式与时间戳的相互转换示例代码
2013/12/25 Javascript
jQuery绑定事件不执行但alert后可以正常执行
2014/06/03 Javascript
Dojo Javascript 编程规范 规范自己的JavaScript书写
2014/10/26 Javascript
Javascript 多物体运动的实现
2014/12/24 Javascript
angular.foreach 循环方法使用指南
2015/01/06 Javascript
使用VS开发 Node.js指南
2015/01/06 Javascript
jQuery中has()方法用法实例
2015/01/06 Javascript
JS继承之借用构造函数继承和组合继承
2016/09/07 Javascript
微信小程序 生命周期详解
2016/10/12 Javascript
深入理解vue中slot与slot-scope的具体使用
2018/01/26 Javascript
详解Vue基于 Nuxt.js 实现服务端渲染(SSR)
2018/04/05 Javascript
微信小程序之多列表的显示和隐藏功能【附源码】
2018/08/06 Javascript
JS面试题大坑之隐式类型转换实例代码
2018/10/14 Javascript
详解利用eventemitter2实现Vue组件通信
2019/11/04 Javascript
python 接口返回的json字符串实例
2018/03/27 Python
Django 实现前端图片压缩功能的方法
2019/08/07 Python
YUV转为jpg图像的实现
2019/12/09 Python
python GUI库图形界面开发之PyQt5控件QTableWidget详细使用方法与属性
2020/02/25 Python
python库skimage给灰度图像染色的方法示例
2020/04/27 Python
台湾网购生鲜第一品牌:i3Fresh爱上新鲜
2017/10/26 全球购物
儿子婚宴答谢词
2014/01/09 职场文书
回门宴答谢词
2014/01/13 职场文书
竞选部门副经理的自荐书范文
2014/02/11 职场文书
新闻传媒系求职信范文
2014/04/19 职场文书
党员干部观看《周恩来四个昼夜》思想汇报
2014/09/10 职场文书
525心理健康活动总结
2015/05/08 职场文书
七夕情人节问候语
2015/11/11 职场文书
2016教师节感恩话语
2015/12/09 职场文书
2019年中,最受大众欢迎的6本新书
2019/08/07 职场文书
聊聊CSS粘性定位sticky案例解析
2022/06/01 HTML / CSS