实现自动打卡的Python程序通常需要以下几个步骤:
1. 获取登录所需的用户名和密码,以及登录后的token值。
2. 使用Selenium或其他自动化工具打开浏览器并登录到打卡系统。
3. 定位打卡按钮并执行打卡操作。
4. 截图保存打卡信息。
5. 发送邮件通知打卡完成。
```python
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from PIL import Image
from bs4 import BeautifulSoup
import requests
import time
from datetime import datetime
from aip import AipOcr
class DailyAttend(object):
def __init__(self, browser, stu_id, passwd, t, address, tmp_yesterday, tmp_today):
self.browser = browser
self.stu_id = stu_id
self.passwd = passwd
self.t = t
self.address = address
self.tmp_yesterday = tmp_yesterday
self.tmp_today = tmp_today
self.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_path
def 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() 或者使用其他浏览器驱动,如Firefox
attendee = DailyAttend(browser, "your_student_id", "your_password", "token_value", "your_address", "yesterday_date", "today_date")
attendee.run()
请注意,这个示例代码需要根据实际的网页结构和元素ID进行调整。另外,验证码的识别可能需要使用第三方OCR服务,如百度AI的OCR API。
还需要注意,自动化打卡可能违反了许多教育机构的使用条款,使用前请确保你有合法的理由和权限。