核酸管理网站爬取

点击核减删除用的代码

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!")

不用点核减删除,直接生成两个文件

from selenium import webdriver
from selenium.webdriver.common.by import By
import time
import datetime


list_no = []      # 存放没有完成人员的列表,格式为 出生年4位+身份证最后3位+姓名
dict_class = {}   # 读文件建立字典,键是 出生年4位+身份证最后3位+姓名, 值是班级
with open("name_class.txt", 'r') as frd:
    for line in frd:
        dw = line.strip().split('\t')
        dict_class[dw[0]] = dw[1]
        
# date = str(datetime.date.today()-datetime.timedelta(1)) # 日期是昨天,用于次日一早检测昨天没有完成的
date = str(datetime.date.today())    # 日期是今天,用于检测当天没有完成的
str_date = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M') # 现在的日期时间

driver = webdriver.Chrome()
driver.implicitly_wait(20)
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()  # 点击登陆

iframe = driver.find_element(By.ID, "mainFrame")      # 切换到iframe
driver.switch_to.frame(iframe)

driver.find_element(By.XPATH, '//button/span[@class="page-size"]').click()   # 找到每页显示20按钮,点击
driver.find_element(By.XPATH, '//span[@class="page-list"]/span/ul/li[5]').click()  # 找到每页显示500,点击

fo = open("all" + str_date + ".txt", "w", 2048)                          # fo 文件输出全部人员的 姓名 身份证 本周次数 核检日期 时间

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:
    list_ = td.text.split(' ')  # 对tr标签的文本按照空格切割得到字符串列表,列表元素:姓名 身份证号 手机 地址 类别  日期 时间 次数 分组
    index = len(list_) - 4      # 地址可能没有,或者有,或者有两个(因为地址中间有空格),index 指向 日期
    fo.write(list_[0] + "\t" + list_[1] + "\t" + list_[index-1] + "\t" + list_[index] + "\t" + list_[index+2]  + "\n" )
    if list_[index]!= date:            # 当天没有做核酸
        list_no.append(list_[1][6:10] + list_[1][15:] + list_[0])  # 格式为出生年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")   # 产生本页存放人员信息的tr标签的列表
for td in points:
    list_ = td.text.split(' ')  
    index = len(list_) - 4
    fo.write(list_[0] + "\t" + list_[1] + "\t" + list_[index-1] + "\t" + list_[index] + "\t" + list_[index+2]  + "\n" )
    if list_[index]!= date:
        list_no.append(list_[1][6:10] + list_[1][15:] + list_[0]) 
        
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")   # 产生本页存放人员信息的tr标签的列表
for td in points:
    list_ = td.text.split(' ')  
    index = len(list_) - 4
    fo.write(list_[0] + "\t" + list_[1] + "\t" + list_[index-1] + "\t" + list_[index] + "\t" + list_[index+2]  + "\n" )
    if list_[index]!= date:
        list_no.append(list_[1][6:10] + list_[1][15:] + list_[0]) 
fo.close()
list_no.sort()
with open("no" + str_date + ".txt", 'w') as fw:
    for ren in list_no:
        if int(ren[:4]) < 2002 :
            fw.write(ren[7:] + '\n')
        else:
            fw.write(ren[7:] + '\t' + dict_class.get(ren, "未知") + '\n')
print("end!")

用显示等待的代码

from selenium import webdriver
from selenium.webdriver.common.by import By
import time
import datetime
from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC

list_no = []      # 存放没有完成人员的列表,格式为 出生年4位+身份证最后3位+姓名
dict_class = {}   # 读文件建立字典,键是 出生年4位+身份证最后3位+姓名, 值是班级
with open("name_class.txt", 'r') as frd:
    for line in frd:
        dw = line.strip().split('\t')
        dict_class[dw[0]] = dw[1]
        
# date = str(datetime.date.today()-datetime.timedelta(1)) # 日期是昨天,用于次日一早检测昨天没有完成的
date = str(datetime.date.today())    # 日期是今天,用于检测当天没有完成的
str_date = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M') # 现在的日期时间

driver = webdriver.Chrome()
# driver.implicitly_wait(20)
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()  # 点击登陆

iframe = driver.find_element(By.ID, "mainFrame")      # 切换到iframe
driver.switch_to.frame(iframe)

driver.find_element(By.XPATH, '//button/span[@class="page-size"]').click()   # 找到每页显示20按钮,点击
driver.find_element(By.XPATH, '//span[@class="page-list"]/span/ul/li[5]').click()  # 找到每页显示500,点击
                         
with open("all" + str_date + ".txt", 'w',2048) as fo:  # fo 文件输出全部人员的 姓名 身份证 本周次数 核检日期 时间
    
    locator = (By.XPATH,"//*[@id='rightTable']/tbody/tr")
    WebDriverWait(driver,20).until(EC.presence_of_element_located(locator))  # 显示等待,直到元素出现

    time.sleep(2)   
    points = driver.find_elements(By.XPATH, "//*[@id='rightTable']/tbody/tr")   # 产生本页存放人员信息的tr标签的列表
    for td in points:
        list_ = td.text.split(' ')  # 对tr标签的文本按照空格切割得到字符串列表,列表元素:姓名 身份证号 手机 地址 类别  日期 时间 次数 分组
        index = len(list_) - 4      # 地址可能没有,或者有,或者有两个(因为地址中间有空格),index 指向 日期
        fo.write(list_[0] + "\t" + list_[1] + "\t" + list_[index-1] + "\t" + list_[index] + "\t" + list_[index+2]  + "\n" )
        if list_[index]!= date:            # 当天没有做核酸
            list_no.append(list_[1][6:10] + list_[1][15:] + list_[0])  # 格式为出生年4位+身份证最后3位+姓名

    driver.find_element(By.LINK_TEXT, "2").click()    # 找到第二页按钮,点击
    # WebDriverWait(driver,20).until(EC.presence_of_element_located(locator))
    WebDriverWait(driver,20).until(lambda _:driver.find_elements(By.XPATH,"//*[@id='rightTable']/tbody/tr")) # 显示等待,直到元素出现
    time.sleep(2)
    points = driver.find_elements(By.XPATH, "//*[@id='rightTable']/tbody/tr")   # 产生本页存放人员信息的tr标签的列表
    for td in points:
        list_ = td.text.split(' ')  
        index = len(list_) - 4
        fo.write(list_[0] + "\t" + list_[1] + "\t" + list_[index-1] + "\t" + list_[index] + "\t" + list_[index+2]  + "\n" )
        if list_[index]!= date:
            list_no.append(list_[1][6:10] + list_[1][15:] + list_[0]) 

    driver.find_element(By.LINK_TEXT, "3").click()            # 找到第三页按钮,点击
    # WebDriverWait(driver,20).until(EC.presence_of_element_located(locator))
    WebDriverWait(driver,20).until(lambda _:driver.find_elements(By.XPATH,"//*[@id='rightTable']/tbody/tr"))
    time.sleep(2)
    points = driver.find_elements(By.XPATH, "//*[@id='rightTable']/tbody/tr")   # 产生本页存放人员信息的tr标签的列表
    for td in points:
        list_ = td.text.split(' ')  
        index = len(list_) - 4
        fo.write(list_[0] + "\t" + list_[1] + "\t" + list_[index-1] + "\t" + list_[index] + "\t" + list_[index+2]  + "\n" )
        if list_[index]!= date:
            list_no.append(list_[1][6:10] + list_[1][15:] + list_[0]) 

list_no.sort()
with open("no" + str_date + ".txt", 'w',2048) as fw:
    for ren in list_no:
        if int(ren[:4]) < 2002 :
            fw.write(ren[7:] + '\n')
        else:
            fw.write(ren[7:] + '\t' + dict_class.get(ren, "未知") + '\n')
driver.close()
print("end!")

复制网站每个人信息运行程序处理好的信息放入剪切板

'''
程序功能:选定并复制好网站人员的信息后,运行本程序,剪切板里面是人员名单和班级
程序进入条件:
    name_class.txt:存放人员id及班级信息,格式为 2009216魏子旭	8.1 (身份证年加身份证后3位)
    剪切板里面的复制格式:每行格式为 	刘迎奥	3708282007*****239	159****4892		学生	2022-10-05 06:32:24	3	8.5班
程序输出时剪板里面的格式:名字 班级
'''

import pyperclip

list_id = []
str_temp = pyperclip.paste()
list_lines = str_temp.split("\n")
for line in list_lines:
    list_ = line.strip().split("\t",2)
    list_id.append(list_[1][6:10] + list_[1][15:] + list_[0])
list_id.sort()
dict_ = {}
with open("name_class.txt", 'r') as frd:
    for line in frd:
        line = line.strip()
        listd = line.split('\t')
        dict_[listd[0]] = listd[1]
str_temp = ""
for ren in list_id:
    if int(ren[:4]) < 2002 :
        str_temp += ren[7:] + '\n'
    else:
        str_temp += ren[7:] + '\t' + dict_.get(ren, "未知") + '\n'

pyperclip.copy(str_temp)

posted @   越自律越自由  阅读(231)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· 【.NET】调用本地 Deepseek 模型
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
点击右上角即可分享
微信分享提示