python如何实现自动打卡小程序
短信预约 -IT技能 免费直播动态提醒
这篇文章主要介绍了python如何实现自动打卡小程序,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
具体内容如下
from selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsfrom PIL import Imagefrom bs4 import BeautifulSoupimport requestsfrom aip import AipOcrimport timefrom datetime import datetimeimport reclass 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_class_name("vcdoe-tips").click() # 模拟点击使验证码加载出来 time.sleep(2) webpage = self.browser.page_source soup = BeautifulSoup(webpage, features="html.parser") div = soup.find("div", attrs={"class": "login-content"}) class="lazy" data-src = div.find_all("img")[2].attrs["class="lazy" data-src"] # 从html中解析出图片链接 r = requests.get(class="lazy" data-src) if r.status_code == 200: open(self.img_path, "wb").write(r.content) else: print("网络不佳,无法加载验证码图片") def recog_captcha_img(self): img = Image.open(self.img_path) img = img.convert('L') # P模式转换为L模式(灰度模式默认阈值127) count = 165 # 设定阈值 table = [] for i in range(256): if i < count: table.append(0) else: table.append(1) img = img.point(table, '1') img.save(self.img_path) # 保存处理后的验证码 ## 调用百度ocr # 识别码 APP_ID = "23779944" API_KEY = "FPgsSXsuqXk3twpqVHmNNK6g" SECRET_KEY = "nG08oGzErk8CdMvDAynAiGdzfBjHr3NO" # 初始化对象 client = AipOcr(APP_ID, API_KEY, SECRET_KEY) # 读取图片 def get_file_content(file_path): with open(file_path, 'rb') as f: return f.read() image = get_file_content(self.img_path) # 定义参数变量 options = {'language_type': 'ENG', } # 识别语言类型,默认为'CHN_ENG'中英文混合 # 调用通用文字识别 result = client.basicGeneral(image, options) # 高精度接口 basicAccurate for word in result['words_result']: self.captcha = (word['words']) def login(self): ## 登录 while True: self.browser.find_element_by_css_selector("[type=text]").send_keys(self.stu_id) self.browser.find_element_by_css_selector("[type=password]").send_keys(self.passwd) self.browser.find_element_by_css_selector("[type=number]").send_keys(self.captcha) self.browser.find_element_by_tag_name("button").click() time.sleep(2) page = self.browser.page_source html = BeautifulSoup(page, features="html.parser") err_message = html.find("p", attrs={"class": "el-message__content"}) if err_message.text == "登录成功": print("登录成功!") break elif err_message.text == "账号或密码错误": print("账号或密码错误!请重新输入!") self.stu_id = input("请输入学号:") self.passwd = input("请输入密码:") continue else: self.get_captcha_img() self.recog_captcha_img() continue def attend(self): success_messages = self.browser.find_elements_by_css_selector('p[class=el-message__content]') success_messages = [message.text for message in success_messages] if "今日已打卡" in success_messages: print("今日已打卡!") time.sleep(60) else: ## 选择打卡定位 self.browser.find_elements_by_xpath('//div/span[text()="正在获取定位..."]')[1].click() time.sleep(1) for i in range(17): self.browser.find_elements_by_xpath('//ul/li')[i + 1].click() time.sleep(1) self.browser.find_element_by_xpath('//ul/li[text()="岳麓区"]').click() time.sleep(1) self.browser.find_element_by_xpath('//div/button[text()="确认"]').click() time.sleep(1) ## 输入相关打卡信息并点击打卡按钮 self.browser.find_elements_by_css_selector('input[placeholder="街道门牌、楼层房间号等信息"]')[1].send_keys(self.address) temp = self.browser.find_elements_by_css_selector("input[placeholder=请输入]") temp[0].send_keys(self.tmp_yesterday) temp[1].send_keys(self.tmp_today) self.browser.find_elements_by_css_selector( 'button[class="btnDaka van-button van-button--info van-button--normal van-button--block"]')[1].click() today = datetime.now().strftime("%Y-%m-%d") print(today + "打卡成功!") time.sleep(60)if __name__ == "__main__": ## 欢迎界面 print("=" * 100) print("打卡小程序") print("欢迎你湖南大学的朋友!开始使用吧!") print("=" * 100) ## 用户输入 while True: t = input("请输入你的每日打卡时间(24小时制, 例如:00:10):") if re.search('^(([0-1][0-9])|(2[1-3])):[0-5][0-9]$', t) == None: print("你输入的时间格式有误,请重新输入!") continue stu_id = input("请输入你的学号:") passwd = input("请输入个人门户密码:") address = input("请输入你的打卡详细地址(例如:湖南大学北校区1舍):") tmp_yesterday = input("请输入你的昨日体温:") tmp_today = input("请输入你的今日体温:") print("=" * 100) if input("请检查你的输入是否无误,若有误则输入y并重新输入,若无误则输入n:") == "n": print("=" * 100) break user_info = { 't': t, 'stu_id': stu_id, 'passwd': passwd, 'address': address, 'tmp_yesterday': tmp_yesterday, 'tmp_today': tmp_today } ## 浏览器设置 chrome_options = Options() chrome_options.add_argument("--headless") chrome_options.add_argument("--disable-gpu") chrome_options.add_experimental_option('excludeSwitches', ['enable-logging']) browser = webdriver.Chrome(executable_path="chromedriver.exe", options=chrome_options) ## 打卡 app = DailyAttend(browser, **user_info) # 实例化打卡器 print("正在等待打卡时间到来...") while True: if datetime.now().strftime("%H:%M") == t: app.get_captcha_img() app.recog_captcha_img() app.login() app.attend() else: time.sleep(10)
感谢你能够认真阅读完这篇文章,希望小编分享的“python如何实现自动打卡小程序”这篇文章对大家有帮助,同时也希望大家多多支持编程网,关注编程网行业资讯频道,更多相关知识等着你来学习!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341