初始化selenium
import time
import ddddocr
from selenium import webdriver
from PIL import Image
Host = "https://www.jxjypt.cn"
browser = webdriver.Chrome('./utils/chromedriver.exe')
browser.get(Host + '/hnlgdx')
查找登录按钮
login_btn = browser.find_element_by_xpath('//*[@id="top"]/div/span/a')
login_btn.click()
查找用户名输入框
username = browser.find_element_by_id('txtaccount')
username.send_keys('4108************1557'
)
查找密码输入框
pwd = browser.find_element_by_id('txtpassword')
pwd.send_keys('271557')
while True:
# 获取短信验证码图片链接
code_picture = browser.find_element_by_id('code')
time.sleep(2.5)
browser.get_screenshot_as_file('./status/page.png')
left = int(code_picture.location['x'])
top = int(code_picture.location['y'])
right = int(code_picture.location['x'] + code_picture.size['width'])
bottom = int(code_picture.location['y'] + code_picture.size['height'])
im = Image.open('./status/page.png')
im = im.crop((left, top, right, bottom))
im.save('./status/code.png')
ocr = ddddocr.DdddOcr()
with open(r'./status/code.png', 'rb') as f:
img_bytes = f.read()
res = ocr.classification(img_bytes)
time.sleep(1)
txt_code = browser.find_element_by_id('txtCode')
time.sleep(1)
txt_code.send_keys(res)
time.sleep(1)
# 获取登录按钮
browser.find_element_by_id('btlogin').click()
time.sleep(1)
try:
txt_content = browser.find_element_by_class_name('form-error-text').text
time.sleep(1)
except:
break
获取2021学年下学期按钮
browser.find_element_by_xpath('//*[@id="myTab"]/li[4]/a').click()
courses = browser.find_elements_by_xpath('//*[@id="myTab_Content3"]/ul/li/div[2]/a[1][text()="开始学习"]')
time.sleep(1)
for course in courses:
try:
course.click()
time.sleep(1)
browser.switch_to_window(browser.window_handles[1])
# 获取章节信息
chapters = browser.find_elements_by_class_name('course-list-txt')
time.sleep(1)
# for chapter in chapters:
# chapter.click()
hours = browser.find_elements_by_class_name('z-gery-icon')[1:]
time.sleep(1)
for hour in hours:
hour.click()
try:
icon = hour.find_element_by_class_name('fa-check-circle')
except:
time.sleep(10)
browser.find_element_by_class_name('zkjx').click()
time.sleep(1)
wenzi = str(browser.find_element_by_class_name('wenzi').text).strip()
time.sleep(1)
browser.find_element_by_xpath(f'//dl/dd/span[text()="{wenzi}."]').click()
time.sleep(1)
else:
time.sleep(1)
continue
browser.close()
browser.switch_to_window(browser.window_handles[0])
time.sleep(10)
except Exception as e:
pass
# 获取章节信息
chapters = browser.find_elements_by_class_name(‘course-list-txt’)
time.sleep(1)
# for chapter in chapters:
# chapter.click()
hours = browser.find_elements_by_class_name(‘z-gery-icon’)[1:]
time.sleep(1)
for hour in hours:
hour.click()
try:
icon = hour.find_element_by_class_name(‘fa-check-circle’)
except:
time.sleep(10)
browser.find_element_by_class_name(‘zkjx’).click()
time.sleep(1)
wenzi = str(browser.find_element_by_class_name(‘wenzi’).text).strip()
time.sleep(1)
browser.find_element_by_xpath(f’//dl/dd/span[text()="{wenzi}."]’).click()
time.sleep(1)
else:
time.sleep(1)
continue
browser.close()
browser.switch_to_window(browser.window_handles[0])