利用Python写出疫情每日打卡的小程序

笔者所在的学校为ZJU,如果读者也是的话,那么就可以直接使用了
原理是利用selenium模拟浏览器的操作,然后利用Xpath进行定位,依次把所有的选项都Click掉
下面上代码

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
import openpyxl


url = 'https://healthreport.zju.edu.cn/ncov/wap/default/index'
wb = openpyxl.load_workbook('Database.xlsx')
sheet = wb['Sheet1']

# 扫描长度
length = 1
while sheet.cell(row=length, column=1).value != None:
    length += 1
length -= 1

# 导入姓名数据并模拟打卡
for i in range(length):
    i += 1
    user, pwd = sheet.cell(row=i, column=2).value, sheet.cell(row=i, column=3).value

    driver = webdriver.Chrome(r'C:\Users\Intel\Desktop\- Career -\My_Coding\chromedriver.exe')
    driver.get(url)
    driver.find_element_by_xpath('//*[@id="username"]').send_keys(user)
    driver.find_element_by_xpath('//*[@id="password"]').send_keys(pwd)
    driver.find_element_by_xpath('//*[@id="password"]').send_keys(Keys.ENTER)
    driver.get(url)
    print(sheet.cell(row=i, column=1).value + ':成功进入网站', end='\n')

    XpathStorage = ['/html/body/div[1]/div[1]/div/section/div[4]/ul/li[4]/div/div/div[1]/span[2]',
                    '/html/body/div[1]/div[1]/div/section/div[4]/ul/li[5]/div/div/div[5]/span[2]',
                    '/html/body/div[1]/div[1]/div/section/div[4]/ul/li[6]/div/div/div[1]/span[2]',
                    '/html/body/div[1]/div[1]/div/section/div[4]/ul/li[7]/div/div/div[2]/span[2]',
                    '/html/body/div[1]/div[1]/div/section/div[4]/ul/li[8]/div/div/div[2]/span[2]',
                    '/html/body/div[1]/div[1]/div/section/div[4]/ul/li[9]/div/div/div[2]/span[2]',
                    '/html/body/div[1]/div[1]/div/section/div[4]/ul/li[14]/div/div/div[1]/span[2]',
                    '/html/body/div[1]/div[1]/div/section/div[4]/ul/li[13]/div/div/div[1]/span[2]',
                    '/html/body/div[1]/div[1]/div/section/div[4]/ul/li[14]/div/div/div[1]/span[2]',
                    '/html/body/div[1]/div[1]/div/section/div[4]/ul/li[13]/div/div/div[1]/span[2]',
                    '/html/body/div[1]/div[1]/div/section/div[4]/ul/li[15]/div/div/div[1]/span[2]'
                    ]
    try:
        for i in XpathStorage:
            driver.find_element_by_xpath(i).click()

    except:
        print('Cant input')

# 获取地点数据
    element2 = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div/section/div[4]/ul/li[19]/div/input')
    driver.execute_script("arguments[0].click();", element2)

    sleep(0.5)
    try:
        driver.find_element_by_xpath(
            '/html/body/div[1]/div[1]/div/section/div[4]/ul/li[22]/div/div/div[2]/span[2]').click()
        driver.find_element_by_xpath(
            '/html/body/div[1]/div[1]/div/section/div[4]/ul/li[34]/div/div/div/span[2]').click()
        print('地点未变更,完成填写')
    except:
        try:
            driver.find_element_by_xpath(
                '/html/body/div[1]/div[1]/div/section/div[4]/ul/li[21]/div/div/div[6]/span[2]').click()
            driver.find_element_by_xpath(
                '/html/body/div[1]/div[1]/div/section/div[4]/ul/li[23]/div/div/div[2]/span[2]').click()
            driver.find_element_by_xpath(
                '/html/body/div[1]/div[1]/div/section/div[4]/ul/li[35]/div/div/div/span[2]').click()
            print('地点变更,完成填写')
        except:
            print('出现未知错误')

# 提交按钮
    driver.find_element_by_xpath('/html/body/div[1]/div[1]/div/section/div[5]/div/a').click()

    try:
        driver.find_element_by_xpath('//*[@id="wapcf"]/div/div[2]/div[2]').click()
    except:
        try:
            driver.find_element_by_xpath('//*[@id="wapat"]/div/div[2]/div').click()
        except:
            print('提交出错')
        else:
            print('今天已填写过')
    else:
        print('填写成功')

    driver.quit()

wb.close()
print("程序运行结束")

笔者把所有要打卡的账号数据从excel中导入,因此使用了openpyxl模块,如果大家只需要自己打卡,那么把user和pwd改成自己的学号和密码就行了。

发表评论