写一个python脚本用来自动签到,使用dddoocr识别验证码
1、获取到验证码图片,使用get方式,链接为https://yabook.blog/e/ShowKey/?v=login&t=0.5317634968437364
2、验证码为4位数字,使用自动识别图片的方式识别出验证码,并且获取到返回的cookie
3、登录,登录链接为https://yabook.blog/e/member/doaction.php, 使用post方式登录,提交文本为ecmsfrom=&enews=login&tobind=0&username=chump&password=123456&lifetime=86400&key=7988&is_ajax=1,其中key值后面的数字为验证码,需要带入获取验证码返回的cookie访问,如果登录成功,在返回的源码中会有"登录成功"字样,如果提示"验证码不正确"就需要重新获取验证码并识别再登录,登录成功后需要获取返回的完整cookie
4、签到,get访问验证码链接https://yabook.blog/e/ShowKey/?v=spacefb, 得到验证码图片,并识别验证码,以及获取到cookie
5、使用登录成功后的cookie以及签到获取到的验证码cookie组合后开始签到,签到链接为https://yabook.blog/e/member/cp/qiandao.php?action=qiandao, post访问,提交文本为key=4807,其中key为获取到的签到验证码,如果签到成功则返回源码中会包含"签到成功"
import requests
import ddddocr
import time
from http.cookies import SimpleCookie
class AutoCheckIn:
def __init__(self):
# 初始化OCR对象(关闭广告显示)
self.ocr = ddddocr.DdddOcr(show_ad=False)
# 登录相关URL和参数
self.login_captcha_url = "https://yabook.blog/e/ShowKey/?v=login&t=0.5317634968437364"
self.login_url = "https://yabook.blog/e/member/doaction.php"
self.login_data = {
"ecmsfrom": "",
"enews": "login",
"tobind": "0",
"username": "chump",
"password": "xX300400",
"lifetime": "86400",
"is_ajax": "1"
}
# 签到相关URL和参数
self.checkin_captcha_url = "https://yabook.blog/e/ShowKey/?v=spacefb"
self.checkin_url = "https://yabook.blog/e/member/cp/qiandao.php?action=qiandao"
# 会话对象,用于保持cookies
self.session = requests.Session()
def get_captcha_and_cookie(self, url):
"""
获取验证码图片和cookie
:param url: 验证码URL
:return: (验证码文本, cookie字典)
"""
try:
# 发送GET请求获取验证码图片
response = self.session.get(url)
response.raise_for_status()
# 使用ddddocr识别验证码
captcha = self.ocr.classification(response.content)
# 清理识别结果,只保留数字
captcha = ''.join(filter(str.isdigit, captcha))
# 确保识别结果是4位数字
if len(captcha) != 4:
print(f"验证码识别错误: {captcha}")
return None, None
return captcha, response.cookies.get_dict()
except Exception as e:
print(f"获取验证码失败: {e}")
return None, None
def login(self, max_retries=3):
"""
登录网站
:param max_retries: 最大重试次数
:return: 登录成功返回True,否则返回False
"""
retry_count = 0
while retry_count < max_retries:
# 获取登录验证码和cookie
captcha, cookie = self.get_captcha_and_cookie(self.login_captcha_url)
if not captcha or not cookie:
retry_count += 1
time.sleep(1)
continue
print(f"尝试登录,验证码: {captcha}")
# 准备登录数据
login_data = self.login_data.copy()
login_data["key"] = captcha
try:
# 发送POST请求登录
response = self.session.post(
self.login_url,
data=login_data,
cookies=cookie
)
response.raise_for_status()
# 检查登录结果
if "登录成功" in response.text:
print("登录成功!")
print("登录返回的完整cookie:", self.session.cookies.get_dict())
return True
elif "验证码不正确" in response.text:
print("验证码错误,重新尝试...")
retry_count += 1
time.sleep(1)
else:
print("登录失败:", response.text)
retry_count += 1
time.sleep(1)
except Exception as e:
print(f"登录请求失败: {e}")
retry_count += 1
time.sleep(1)
print(f"登录失败,尝试{max_retries}次后仍未成功")
return False
def check_in(self, max_retries=3):
"""
执行签到操作
:param max_retries: 最大重试次数
:return: 签到成功返回True,否则返回False
"""
if not self.session.cookies.get_dict():
print("请先登录再签到")
return False
retry_count = 0
while retry_count < max_retries:
# 获取签到验证码和cookie
captcha, cookie = self.get_captcha_and_cookie(self.checkin_captcha_url)
if not captcha or not cookie:
retry_count += 1
time.sleep(1)
continue
print(f"尝试签到,验证码: {captcha}")
try:
# 准备签到数据
checkin_data = {"key": captcha}
# 合并登录cookie和签到验证码cookie
merged_cookies = {**self.session.cookies.get_dict(), **cookie}
# 发送POST请求签到
response = self.session.post(
self.checkin_url,
data=checkin_data,
cookies=merged_cookies
)
response.raise_for_status()
# 检查签到结果
if "签到成功" in response.text:
print("签到成功!")
print("签到返回的完整cookie:", self.session.cookies.get_dict())
return True
elif "验证码不正确" in response.text:
print("验证码错误,重新尝试...")
retry_count += 1
time.sleep(1)
else:
print("签到失败:", response.text)
retry_count += 1
time.sleep(1)
except Exception as e:
print(f"签到请求失败: {e}")
retry_count += 1
time.sleep(1)
print(f"签到失败,尝试{max_retries}次后仍未成功")
return False
def run(self):
"""
执行完整的签到流程
"""
# 第一步:登录
if self.login():
# 第二步:签到
self.check_in()
if __name__ == "__main__":
auto_checkin = AutoCheckIn()
auto_checkin.run()
0