diff --git a/get_qrcode/main.py b/get_qrcode/main.py index f308d7c..89c5f43 100644 --- a/get_qrcode/main.py +++ b/get_qrcode/main.py @@ -10,16 +10,21 @@ from selenium.webdriver.common.by import By # from selenium.webdriver.support import expected_conditions # from selenium.webdriver.support.wait import WebDriverWait CRAWL_INTERVAL = 0.5 +DRIVER_PATH = "C:/Users/Administrator/Documents/chromedriver_win32/chromedriver.exe" + +logging.basicConfig(level=logging.ERROR, filename="C:/Users/Administrator/Desktop/error.log", + format="%(asctime)s - %(name)s - %(levelname)s - %(message)s") def get_qrcode(): - service = webdriver.ChromeService(executable_path="C:/Users/Administrator/Documents/chromedriver_win32/chromedriver.exe") + service = webdriver.ChromeService(executable_path=DRIVER_PATH) driver = webdriver.Chrome(service=service) redis_client = redis.StrictRedis(host="localhost", port=6379, db=0) driver.get("https://icme.haoyisheng.com") json_str_temp = "" while True: try: + # 判断是否打开二维码页面 qrcode_tab = driver.find_element(By.CSS_SELECTOR, ".x-tab-strip-active .x-tab-strip-text") if qrcode_tab.get_attribute("innerHTML") != "课题二维码": json_str = json.dumps({"qrcode": None}) @@ -37,6 +42,9 @@ def get_qrcode(): # 获取二维码src qrcode_img = driver.find_element(By.CSS_SELECTOR, "#imgDiv #img") qrcode_src = qrcode_img.get_attribute("src") + # TODO: 如果二维码src是http链接,截图转换base64 + # if qrcode_src.startswith("http"): + # qrcode_src = f"data:image/png;base64,{qrcode_img.screenshot_as_base64}" # 课题名称,起止时间 topic_title = driver.find_element(By.CSS_SELECTOR, "#title h1:first-child").text start_time = driver.find_element(By.CSS_SELECTOR, "#title #p1").text @@ -50,6 +58,7 @@ def get_qrcode(): print("二维码已更新", topic_title, start_time, end_time) driver.switch_to.default_content() + # 二维码未找到错误 except NoSuchElementException: json_str = json.dumps({"qrcode": None}) if json_str != json_str_temp: @@ -63,11 +72,20 @@ def get_qrcode(): time.sleep(CRAWL_INTERVAL) continue + # 关闭窗口时 except NoSuchWindowException: print("窗口已关闭") json_str = json.dumps({"qrcode": None}) if json_str != json_str_temp: - json_str_temp = json_str + redis_client.publish("qrcode", json_str) + break + + # 其他错误 + except Exception as e: + print(f"其他错误{e}") + logging.error(f'An error occurred: {e}', exc_info=True) + json_str = json.dumps({"qrcode": None}) + if json_str != json_str_temp: redis_client.publish("qrcode", json_str) break time.sleep(CRAWL_INTERVAL) diff --git a/pyproject.toml b/pyproject.toml index bacff72..2ddf1ee 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "get-qrcode" -version = "0.9.0" +version = "1.0.1" description = "" authors = ["quantulr <35954003+quantulr@users.noreply.github.com>"] readme = "README.md"