溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

python實(shí)現(xiàn)QQ空間自動(dòng)點(diǎn)贊功能

發(fā)布時(shí)間:2020-10-12 15:41:23 來(lái)源:腳本之家 閱讀:444 作者:不負(fù)長(zhǎng)風(fēng) 欄目:開發(fā)技術(shù)

本文實(shí)例為大家分享了python實(shí)現(xiàn)QQ空間自動(dòng)點(diǎn)贊的具體代碼,供大家參考,具體內(nèi)容如下

項(xiàng)目github地址

使用python實(shí)現(xiàn)qq空間自動(dòng)點(diǎn)贊功能。

需自行安裝庫(kù)并配置環(huán)境。

我想實(shí)現(xiàn)的是每6個(gè)小時(shí)就自動(dòng)更新一次cookie。這也是和網(wǎng)上其他版本相比具有的優(yōu)點(diǎn)。不用手動(dòng)輸入cookie。更加自動(dòng)。(不負(fù)責(zé)任的說(shuō),這個(gè)功能沒有測(cè)試過(guò)。)

程序運(yùn)行方法:將代碼存為.py文件,運(yùn)行即可。

輸入QQ密碼的時(shí)候采用了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'] + ' 點(diǎn)贊')
 
 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)) + ' 條說(shuō)說(shuō)')
 
  return data
 except:
  return []
 
 
def main():
 
 print_time()
 print('程序運(yùn)行...')
 
 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()

這個(gè)程序在本地跑沒有問(wèn)題,但是我希望它能在我的騰訊云服務(wù)器上一直運(yùn)行。

我在遼寧,服務(wù)器在北京,導(dǎo)致登錄qq空間時(shí)會(huì)有滑動(dòng)驗(yàn)證碼。

于是我按照網(wǎng)上的教程,結(jié)合qq空間滑動(dòng)驗(yàn)證碼的實(shí)際情況,實(shí)現(xiàn)了qq空間滑動(dòng)驗(yàn)證碼的破解。

值得一提的是,目前成功率是100%。

有的時(shí)候不能完全重合,但還是會(huì)成功。

具體思路我就不貼出來(lái)了,感興趣的朋友可以私信我。

下面是整合了破解滑動(dòng)驗(yàn)證碼部分的代碼。

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'] + ' 點(diǎn)贊')
 
 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)) + ' 條說(shuō)說(shuō)')
 
  return data
 except:
  return []
 
 
def main():
 
 print_time()
 print('程序運(yùn)行...')
 
 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()

上面兩份代碼整體思路沒問(wèn)題,但是偶爾會(huì)有一些小bug。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI