When use requests to detect the status of the network, it'll exit the program anyway. And I don't know and have no time to fix the issue. So I turn back to selenium to detect the status.

This commit is contained in:
Pyhtagodzilla 2025-06-18 21:55:00 +08:00
parent e0cf820637
commit 2bf04c9e85
2 changed files with 18 additions and 6 deletions

View File

@ -1,5 +1,6 @@
import os
import requests
import time
import selenium.common.exceptions
import urllib3
import logging
@ -81,7 +82,7 @@ class BUCTAU:
return False
@staticmethod
def detect_net():
def detect_net_requests():
# noinspection PyBroadException
try:
detector = requests.get(url="https://www.baidu.com", timeout=5)
@ -91,11 +92,22 @@ class BUCTAU:
else:
logging.info("Network is not available")
return False
except urllib3.exceptions.MaxRetryError:
logging.info("MaxRetryError: False")
return False
except requests.ConnectionError:
logging.info("ConnectionError: False")
return False
except Exception:
return False
return False
@staticmethod
def detect_net(driver):
# noinspection PyBroadException
try:
driver.get("https://tree.buct.edu.cn/index_20.html")
driver.maximize_window()
time.sleep(3) # Wait for the page to load
driver.find_element(By.XPATH, '/html/body/main/section/div[1]/div[7]/button[1]')
logging.info("Have logged in")
return True # Not logged in
except Exception:
logging.warning("Not logged in")
return False

2
run.py
View File

@ -9,7 +9,7 @@ def main():
past_time = time.time()
while True:
if time.time() - past_time > 10.0:
if authenticator.detect_net():
if authenticator.detect_net(driver=driver):
past_time = time.time()
continue
else: