思路
通过抓包获取到签到接口的请求参数,然后通过requests库发送请求,完成签到。
通过抓包可知,空签到大致分为了三步
通过带有cookies的请求发送到签到接口,使用ddddocr识别验证码,完成签到。
Github项目地址 https://github.com/ours1505/HDSkyAutoCheckin
使用方式
[ol]
在浏览器中打开签到页面,在开发者工具中查看请求头,找到cookie字段,将cookie复制到config.ini文件中。
在终端中运行脚本,即可完成签到。(建议通过crontab定时执行)
在终端中查看签到结果。
[/ol]
注意事项
[ol]
[/ol]
[Python] 纯文本查看 复制代码import configparser
from urllib.parse import urlencode
from ddddocr import DdddOcr
import requests
import logging
logging.basicConfig(level=logging.INFO, format='%(levelname)s - %(message)s')
def checkin():
# 实例化DdddOcr对象
ocr = DdddOcr()
config = configparser.ConfigParser(interpolation=None)
config.read('config.ini', encoding='utf-8')
ua = config['default']['UserAgent']
headers = {'User-Agent': ua, 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', }
logging.info(f"正在签到站点: hdsky")
cookies = config.get('hdsky', 'cookie').split('; ')
cookie_dict = {}
for pair in cookies:
key, value = pair.split('=', 1)
cookie_dict[key] = value
while True:
payload = urlencode({'action': 'new'})
response = requests.post(f'https://hdsky.me/image_code_ajax.php', headers=headers, data=payload,
cookies=cookie_dict, verify=False)
if response.status_code == 200 and response.json()['success']:
code = response.json()['code']
response = requests.get(f'https://hdsky.me/image.php?action=regimage&imagehash={code}', headers=headers,
stream=True, verify=False)
if response.status_code == 200:
# 将响应内容保存为bytes对象
image_bytes = response.content
# 使用ocr对象识别图片中的验证码
recognized_text = ocr.classification(image_bytes)
logging.info(f'获取到验证码为:{recognized_text}')
payload = {'action': 'showup', 'imagehash': code, 'imagestring': recognized_text}
response = requests.post(f'https://hdsky.me/showup.php', headers=headers, data=payload,
cookies=cookie_dict, verify=False)
if response.json()['success']:
logging.info(f'站点hdsky.me签到成功')
break
elif response.json()['message'] == 'date_unmatch':
logging.info('站点已经签到')
break
else:
logging.info('验证码识别错误,重试')
if __name__ == '__main__':
checkin()