前言作为某高校党委书记,我负责填写体温,提醒一天中的每个时间段,我我有点累了。我想写一个爬虫帮我完成,虽然我每天写一个爬虫帮我填报表。不过那是直接用别人的学生端登录,但是后台是老师端,两者有点区别。而我对Python几乎是零基础,主要学习的语言是C/C++和JAVA。但是偶然看到一个selenium的技术,这个东西就是模拟点击,也可以算是半个爬虫。但这就是我这次使用的。功能特点程序无障碍登录课堂魔方后台,进入体温上报后台界面。可以通过获取Excel学生的学号和邮箱来判断时间点,然后根据时间点判断该学生是否填写了该时间段的体温。如果没有填写,则发送电子邮件敦促他们填写表格。写代码过程中的坎坷1.从Excel中获取数据。Excel表格如图defread():wb=openpyxl.load_workbook('./xl.xlsx')sheet=wb['Sheet1']active_sheet=wb.activeforiinrange(2,38):studentId=active_sheet.cell(row=i,column=1).valuestudentName=active_sheet.cell(row=i,column=2).valuestudentPost=active_sheet.cell(row=i,column=3).valuestr(studentId)str(studentPost)str(studentName)arr={"studentId":studentId,"studentName":studentName,"studentPost":studentPost}readlists.append(arr)这里用到了openpyxl模块,然后从第2行遍历到excel中第37行,取出每条数据存入一个arr字典,然后将每条字典存入list中。2.模拟点击获取后台数据,然后合并excel获取的字典。2.1模拟点击页面。这里先进入魔方类老师登录界面,然后用selenium用xpath模拟点击体温上报界面,获取界面。易错点:因为这里的页面数据比较多,所以需要休眠一定时间,否则获取不到数据。web=Chrome()web.get("http://banjimofang.com/teacher/login?ref=%2Fteacher")web.find_element_by_xpath('/html/body/div/div/div/form/div[3]/div[1]/input').send_keys("Account")web.find_element_by_xpath('/html/body/div/div/div/form/div[3]/div[2]/input').send_keys("密码")web.find_element_by_xpath('/html/body/div/div/div/form/div[3]/div[4]/button').click()web.find_element_by_xpath('//*[@id="mainarea"]/div[3]/div[1]/a/div/h5').click()web.find_element_by_xpath('/html/body/div/div[2]/div/div/div[7]/div[2]/div/div[1]/a/div[1]').click()web.find_element_by_xpath('/html/body/div/div[2]/div/div/div[8]/a/div[1]/i').click()web.switch_to.window(web.window_handles[-1])time.sleep(5)tr_list=web.find_elements_by_xpath('//*[@id="tmptablebody"]/tr')2.2获取类中的人,并合并到字典中。这里是先获取每一个小列表,然后提取数据,再匹配上面获取的邮箱,整合成一个字典。然后将其添加到列表中。这样的字典是arr={"studentId":studentId,"studentName":studentName,"studentPost":tmpstudentpost,"tem1":tem1,"tem2":tem2,"tem3":tem3}包含学号,姓名,email,以及早、中、晚的体温。易错点:studentID更注重类型。为此改了一晚上,一直比较string==int。fortrintr_list:q=tr.find_element_by_xpath("./td[3]").textifq=="班级":studentId=tr.find_element_by_xpath("./td[2]").textstudentName=tr.find_element_by_xpath("./td[1]").texttem1=tr.find_element_by_xpath("./td[4]").texttem2=tr.find_element_by_xpath("./td[5]").texttem3=tr.find_element_by_xpath("./td[6]").texttmpstudentpost="1"#studentId是strstr(studentId)#print(type(studentId))foriinreadlists:#print(i["studentId"])#print(type(i["studentId"]))ifstr(i["studentId"])==str(studentId):tmpstudentpost=i["studentPost"]breakarr={"studentId":studentId,"studentName":studentName,"studentPost":tmpstudentpost,"tem1":tem1,"tem2":tem2,"tem3":tem3}lists.append(arr)3.确定时间点然后发送邮件3.1获取当前时间并返回状态码这里是时间点为判断,这里是上午0-11、11-16、17-22对应的状态码分别是123。小难度:获取时间defwhichtime():start_time1=datetime.datetime.strptime(str(datetime.datetime.now().date())+'0:00','%Y-%m-%d%H:%M')end_time1=datetime.datetime.strptime(str(datetime.datetime.now().date())+'11:00','%Y-%m-%d%H:%M')start_time2=datetime.datetime.strptime(str(datetime.datetime.now().date())+'11:00','%Y-%m-%d%H:%M')end_time2=datetime.datetime.strptime(str(datetime.datetime.now().date())+'16:00','%Y-%m-%d%H:%M')start_time3=datetime.datetime.strptime(str(datetime.datetime.now().date())+'17:00','%Y-%m-%d%H:%M')end_time3=datetime.datetime.strptime(str(datetime.datetime.now().date())+'22:00','%Y-%m-%d%H:%M')now_time=datetime.datetime.now()ifstart_time1
