实现自动打卡的Python程序通常需要以下几个步骤:
1. 获取登录所需的用户名和密码,以及登录后的token值。
2. 使用Selenium或其他自动化工具打开浏览器并登录到打卡系统。
3. 定位打卡按钮并执行打卡操作。
4. 截图保存打卡信息。
5. 发送邮件通知打卡完成。
from selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsfrom PIL import Imagefrom bs4 import BeautifulSoupimport requestsimport timefrom datetime import datetimefrom aip import AipOcrclass DailyAttend(object):def __init__(self, browser, stu_id, passwd, t, address, tmp_yesterday, tmp_today):self.browser = browserself.stu_id = stu_idself.passwd = passwdself.t = tself.address = addressself.tmp_yesterday = tmp_yesterdayself.tmp_today = tmp_todayself.img_path = "captcha.png"def get_captcha_img(self):url = "https://fangkong.hnu.edu.cn/app//login?redirect=%2Fhome"self.browser.get(url)这里可能需要根据网页结构定位验证码图片例如:self.browser.find_element_by_id("captcha").click()然后保存图片到 self.img_pathdef login(self):使用用户名和密码登录self.browser.find_element_by_id("username").send_keys(self.stu_id)self.browser.find_element_by_id("password").send_keys(self.passwd)self.browser.find_element_by_id("login").click()def signin(self):定位打卡按钮并点击self.browser.find_element_by_id("signin_button").click()def capture_attendance_info(self):截图保存打卡信息self.browser.save_screenshot(self.img_path)def send_email(self):使用OCR识别验证码with open(self.img_path, "rb") as f:img = Image.open(f)client = AipOcr()client.basic_general(img)result = client.basic_general(img)captcha_code = result['words_result']发送邮件通知打卡完成例如使用yagmail发送邮件yag = yagmail.SMTP(user="", password="your-password", host="smtp.example.com", port=587)yag.login()yag.send(to="", subject="打卡完成", contents=f"打卡时间:{datetime.now()},验证码:{captcha_code}")yag.quit()def run(self):self.get_captcha_img()self.login()self.signin()self.capture_attendance_info()self.send_email()使用示例browser = webdriver.Chrome() 或者使用其他浏览器驱动,如Firefoxattendee = DailyAttend(browser, "your_student_id", "your_password", "token_value", "your_address", "yesterday_date", "today_date")attendee.run()
请注意,这个示例代码需要根据实际的网页结构和元素ID进行调整。另外,验证码的识别可能需要使用第三方OCR服务,如百度AI的OCR API。
还需要注意,自动化打卡可能违反了许多教育机构的使用条款,使用前请确保你有合法的理由和权限。

