📜  在Python中使用Selenium检查高中成绩

📅  最后修改于: 2022-05-13 01:55:24.976000             🧑  作者: Mango

在Python中使用Selenium检查高中成绩

我们将使用selenium研究检查高中成绩状态通过或失败。这对学校非常有用,因为当他们检查有多少学生通过失败以及失败学生的名字是什么时。如果学生人数为 10 人且少于 10 人,则通过手动轻松检查,如果学生人数为 400 至 1000 人,则通过手动检查状态非常困难且耗时过程,那么我们正在对结果进行智能工作检查。使用selenium检查结果并存储在 CSV 文件中。

我们将使用selenium检查高中成绩的状态。这可能对学校当局非常有帮助,可以帮助他们根据通过和失败状态组织数据。他们可以得到通过和不及格的学生的数量,然后是他们的名字。只有在学生人数较少的情况下,例如在 10 或 20 人左右,手动检查状态是可取的。但对于许多学生来说,进行手动检查是不推荐和方便的,因为它需要更多的时间和工作。

在这里,我们想出了一个解决方案,使用selenium自动化这个过程。我们可以对结果状态进行智能检查并将数据存储在 CSV 文件中

要求:

您需要安装 chrome 驱动程序并设置路径。点击这里下载。有关更多信息,请点击此链接。

以下是步骤:

  1. 首先去高中网站点击这个链接(这是为了上板高中成绩)。
  2. 然后通过紧急 ctrl + shift + i 或进入浏览器设置并手动单击调查详细信息单击调查元素。
  3. 然后导航到填充卷号的框,然后复制 x_path。
  4. 然后导航提交按钮,然后复制 x_path。
  5. 我想将结果存储在 CSV 文件中,然后导航学生姓名和状态。然后通过脚本自动填写卷号转到下一页找到名称和状态的x_path。

借助截图一步一步地复制元素的x_path并放入代码中:

第1步:

第2步:

第 3 步:

第4步:

第 5 步:

第 6 步:

第 7 步:

第 8 步:

第 9 步:

下面是实现:

Python3
# import required libraries
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import csv 
import time
  
# give name of csv file
filename = "result.csv"
   
# open file in write mode
f = open(filename, 'w')
  
# create header
header = "name,status\n"
  
# write into the file
f.write(header)
  
# put rollnumber without zero like
# your number 0477593 then
# put 477593 upto XXXXX.
start_roll_no = 477593
end_roll_no = 477599
  
for i in range(start_roll_no, end_roll_no + 1):
      
    # exception handling
    try:
        # create instance of Chrome webdriver
        driver = webdriver.Chrome()
  
        # paste the link upboard website
        driver.get("http://upresults.nic.in/PAHS20SS/HighGetRoll.htm")
          
        # roll number start with zero then change into string
        tv= '0'+str(i)
          
        # find the element where we have to
        # enter the xpath target rollnumber box 
        # and put rollnumber value who store in t. 
        driver.find_element_by_xpath("/html/body/center/table/tbody/tr[2]/td/form/p[1]/input").send_keys(t)
          
        # paste the xpath of submit button 
        driver.find_element_by_xpath("/html/body/center/table/tbody/tr[2]/td/form/p[2]/input[1]").click()
  
        # name of student's x_path copy and get text of element
        name=driver.find_element_by_xpath('/html/body/center/table[2]/tbody/tr[2]/td[2]/p/b/font').text
          
        # result of student's x_path copy and get text of element
        status=driver.find_element_by_xpath('/html/body/center/table[3]/tbody/tr[5]/td[6]/b/font').text
          
        # write in the file
        f.write(name + "," + status + "\n")
          
        # close the web driver
        driver.close()
          
    # using exception when rollnumber is wrong and
    # not show result on this roll number
    except NoSuchElementException as exception:
        continue
  
# close and save the file
f.close()


输出:

输出 csv 文件