from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
import time
import datetime
# date = str(datetime.date.today()-datetime.timedelta(1)) # 日期是昨天,用于次日一早检测昨天没有完成的
date = str(datetime.date.today()) # 日期是今天,用于检测当天没有完成的
driver = webdriver.Chrome()
driver.implicitly_wait(30)
driver.get("http://218.59.156.206:8888/bims/")
driver.maximize_window()
driver.find_element(By.ID, "userName").send_keys('') # 账号
driver.find_element(By.NAME, "password").send_keys('') # 密码
driver.find_element(By.XPATH, "//form/div[3]/input").click() # 点击登陆
point = driver.find_element(By.XPATH, "//*[@id='mainSplitter']/div[3]/div/ul/li") # 找到地球图标元素
ac = ActionChains(driver)
ac.click(point).perform() # 点击图标,出现人员管理,核减删除
time.sleep(1)
ac.move_to_element_with_offset(point, 108, 46).click().perform() # 点击核减删除
ac.move_to_element_with_offset(point, 200, 200).perform() # 点击别处,让核减删除消失
time.sleep(1)
iframe = driver.find_element(By.ID, "mainFrame") # 切换到iframe
driver.switch_to.frame(iframe)
ac = ActionChains(driver)
point = driver.find_element(By.XPATH, "//*[@id='toolbar']/div[3]/div/button") # 找到 核减人员,删除人员 按钮 点击
ac.click(point).perform()
time.sleep(1)
ac.move_to_element_with_offset(point, 0, 68).click().perform() # 点击删除人员,让其复选框消失
point = driver.find_element(By.XPATH, '//button/span[@class="page-size"]') # 找到每页显示 按钮,点击
ac.click(point).perform()
time.sleep(1)
ac.move_to_element_with_offset(point, 0, -35).click().perform() # 在出现的每页显示500上点击
fo = open("d:/stu.txt", "w", 2048) # fo 文件输出全部人员的 姓名 身份证 本周次数 核检日期 时间
fe = open("d:/no.txt", "w", 2048) # fe 文件输出本日没有做核酸的人员信息
driver.find_elements(By.XPATH,"//tr[@date-index='0']") # 如果第一个tr找到,说明已经加载完成
points = driver.find_elements(By.XPATH, "//*[@id='rightTable']/tbody/tr") # 产生本页存放人员信息的tr标签的列表
for td in points: # 遍历tr标签列表
list_ = td.text.split(' ') # 对tr标签的文本按照空格切割得到字符串列表,列表元素:姓名 身份证号 手机 地址 次数 日期 时间 ....
index = 3 # 如果没有地址,列表索引3 为次数
if len(list_[4])==1:
index = 4 # 如果有地址,列表索引4 为次数
elif len(list_[5])==1:
index = 5 # 如果有地址但是地址中有空格,列表索引5 为次数
fo.write(list_[0] + "\t" + list_[1] + "\t" + list_[index] + "\t" + list_[index+1] + "\t" + list_[index+2] + "\n" ) # 写入fo文件
if list_[index+1]!= date: # 如果日期不是今天的日期,说明今天没有做,存入fe文件
fe.write(list_[1][6:10] + list_[1][15:] + list_[0] + "\n") # 格式为出生年4位+身份证最后3位+姓名
driver.find_element(By.LINK_TEXT, "2").click() # 找到第二页按钮,点击
driver.find_elements(By.XPATH,"//tr[@date-index='0']")
points = driver.find_elements(By.XPATH, "//*[@id='rightTable']/tbody/tr")
for td in points:
list_ = td.text.split(' ')
index = 3
if len(list_[4])==1:
index = 4
elif len(list_[5])==1:
index = 5
fo.write(list_[0] + "\t" + list_[1] + "\t" + list_[index] + "\t" + list_[index+1] + "\t" + list_[index+2] + "\n" )
if list_[index+1]!= date:
fe.write(list_[1][6:10] + list_[1][15:] + list_[0] + "\n")
driver.find_element(By.LINK_TEXT, "3").click() # 找到第三页按钮,点击
driver.find_elements(By.XPATH,"//tr[@date-index='0']")
points = driver.find_elements(By.XPATH, "//*[@id='rightTable']/tbody/tr")
for td in points:
list_ = td.text.split(' ')
index = 3
if len(list_[4])==1:
index = 4
elif len(list_[5])==1:
index = 5
fo.write(list_[0] + "\t" + list_[1] + "\t" + list_[index] + "\t" + list_[index+1] + "\t" + list_[index+2] + "\n" )
if list_[index+1]!= date:
fe.write(list_[1][6:10] + list_[1][15:] + list_[0] + "\n")
#fe.write(list_[0] + list_[1][15:] + "\n")
# fe.write(list_[0] + "\t" + list_[1] + "\t" + list_[index] + "\t" + list_[index+1] + "\t" + list_[index+2] + "\n")
fo.close()
fe.close()
print("end!")
标签:index,point,网站,list,driver,element,爬取,核酸,find
From: https://www.cnblogs.com/sunwenping/p/16736638.html