Skip to content

Instantly share code, notes, and snippets.

@Warm-rain
Created April 6, 2019 12:07
Show Gist options
  • Save Warm-rain/3ee7005e1416035c250c5f38c18ea282 to your computer and use it in GitHub Desktop.
Save Warm-rain/3ee7005e1416035c250c5f38c18ea282 to your computer and use it in GitHub Desktop.
python
::: hljs-center
**PT自动签到**
:::
已比较棘手的天空为例,脚本python写的,自行搭建环境,下面代码。
```#coding=utf-8
import os
import re
import sys
sys.path.append("C:\\Users\warmsake\AppData\Local\Programs\Python\Python37-32\Lib\site-packages")
from selenium import webdriver
from PIL import Image,ImageEnhance
import pytesseract
import time
import requests
from hashlib import md5
from time import sleep
from selenium.webdriver.chrome.options import Options
chromedriver = 'C:\\Users\warmsake\AppData\Local\Programs\Python\Python37-32\Scripts/chromedriver.exe'
profile_dir = r'C:\Users\warmsake\AppData\Local\Google\Chrome\User Data'#chrome存放cookie的路径
chrome_options = Options()
chrome_options.add_argument('--headless')#调用chrome内核
chrome_options.add_argument("user-data-dir=" + os.path.abspath(profile_dir))
driver = webdriver.Chrome(options=chrome_options)
driver.set_window_size(1280,1080)#设置浏览器窗口大小
sleep(2)
first_url = 'https://hdsky.me/torrents.php'
driver.get(first_url)
sleep(2)
#点击签到
driver.find_element_by_xpath("//*[@id='showup']").click()
time.sleep(5)
# 截取当前网页并放到C盘下命名为printscreen,该网页有我们需要的验证码
driver.save_screenshot('C:\\python\qiandao.png')
imgelement = driver.find_element_by_xpath("//*[@id='showupimg']") # 定位验证码
#定位验证码位置及大小
location = imgelement.location # 获取验证码x,y轴坐标
size = imgelement.size # 获取验证码的长宽
rangle = (int(location['x']), int(location['y']), int(location['x'] + size['width']),
int(location['y'] + size['height'])) # 写成我们需要截取的位置坐标
i = Image.open("C:\\python\qiandao.png") # 打开截图
frame4 = i.crop(rangle) # 使用Image的crop函数,从截图中再次截取我们需要的区域
frame4.save('C:\\python\sky_sign.png') # 保存我们接下来的验证码图片 进行打码
#进行打码
class RClient(object):
def __init__(self, username, password, soft_id, soft_key):
self.username = username
if len(password) == 32:
self.password = password
else:
try:
self.password = md5(password.encode("utf-8")).hexdigest()
except:
print('若快登录失败,请在config.py中,使用md5后的值')
self.soft_id = soft_id
self.soft_key = soft_key
self.base_params = {
'username': '你的若快账号',
'password': md5('若快密码'.encode('utf8')).hexdigest(),
'softid': '114700',
'softkey': 'faa7356cb86f40dcb155208875828cdc',
}
self.headers = {
'Connection': 'Keep-Alive',
'Expect': '100-continue',
'User-Agent': 'ben',
}
def rk_create(self, im, im_type, timeout=60):
"""
im: 图片字节
im_type: 题目类型
"""
params = {
'typeid': im_type,
'timeout': timeout,
}
params.update(self.base_params)
files = {'image': ('a.jpg', im)}
r = requests.post('http://api.ruokuai.com/create.json', data=params, files=files, headers=self.headers)
return r.json()
def rk_report_error(self, im_id):
"""
im_id:报错题目的ID
"""
params = {
'id': im_id,
}
params.update(self.base_params)
r = requests.post('http://api.ruokuai.com/reporterror.json', data=params, headers=self.headers)
return r.json()
if __name__ == '__main__':
rc = RClient('username', 'password', 'soft_id', 'soft_key')
im = open('C:\\python\sky_sign.png', 'rb').read()
rc_result = rc.rk_create(im, 3060)
if 'Result' in rc_result:
code = rc_result['Result']
else:
print(rc_result)
#读取识别验证码
print('签到验证码是:%s'%code)
driver.find_element_by_xpath("//*[@id='imagestring']").send_keys(code)
time.sleep(2)
#提交签到
driver.find_element_by_xpath("//*[@id='showupbutton']").click()
time.sleep(2)
print('签到成功')
#5s后退出
time.sleep(5)
driver.quit()
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment