添加错误日志
This commit is contained in:
@ -10,16 +10,21 @@ from selenium.webdriver.common.by import By
|
|||||||
# from selenium.webdriver.support import expected_conditions
|
# from selenium.webdriver.support import expected_conditions
|
||||||
# from selenium.webdriver.support.wait import WebDriverWait
|
# from selenium.webdriver.support.wait import WebDriverWait
|
||||||
CRAWL_INTERVAL = 0.5
|
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():
|
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)
|
driver = webdriver.Chrome(service=service)
|
||||||
redis_client = redis.StrictRedis(host="localhost", port=6379, db=0)
|
redis_client = redis.StrictRedis(host="localhost", port=6379, db=0)
|
||||||
driver.get("https://icme.haoyisheng.com")
|
driver.get("https://icme.haoyisheng.com")
|
||||||
json_str_temp = ""
|
json_str_temp = ""
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
|
# 判断是否打开二维码页面
|
||||||
qrcode_tab = driver.find_element(By.CSS_SELECTOR, ".x-tab-strip-active .x-tab-strip-text")
|
qrcode_tab = driver.find_element(By.CSS_SELECTOR, ".x-tab-strip-active .x-tab-strip-text")
|
||||||
if qrcode_tab.get_attribute("innerHTML") != "课题二维码":
|
if qrcode_tab.get_attribute("innerHTML") != "课题二维码":
|
||||||
json_str = json.dumps({"qrcode": None})
|
json_str = json.dumps({"qrcode": None})
|
||||||
@ -37,6 +42,9 @@ def get_qrcode():
|
|||||||
# 获取二维码src
|
# 获取二维码src
|
||||||
qrcode_img = driver.find_element(By.CSS_SELECTOR, "#imgDiv #img")
|
qrcode_img = driver.find_element(By.CSS_SELECTOR, "#imgDiv #img")
|
||||||
qrcode_src = qrcode_img.get_attribute("src")
|
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
|
topic_title = driver.find_element(By.CSS_SELECTOR, "#title h1:first-child").text
|
||||||
start_time = driver.find_element(By.CSS_SELECTOR, "#title #p1").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)
|
print("二维码已更新", topic_title, start_time, end_time)
|
||||||
driver.switch_to.default_content()
|
driver.switch_to.default_content()
|
||||||
|
|
||||||
|
# 二维码未找到错误
|
||||||
except NoSuchElementException:
|
except NoSuchElementException:
|
||||||
json_str = json.dumps({"qrcode": None})
|
json_str = json.dumps({"qrcode": None})
|
||||||
if json_str != json_str_temp:
|
if json_str != json_str_temp:
|
||||||
@ -63,11 +72,20 @@ def get_qrcode():
|
|||||||
time.sleep(CRAWL_INTERVAL)
|
time.sleep(CRAWL_INTERVAL)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
# 关闭窗口时
|
||||||
except NoSuchWindowException:
|
except NoSuchWindowException:
|
||||||
print("窗口已关闭")
|
print("窗口已关闭")
|
||||||
json_str = json.dumps({"qrcode": None})
|
json_str = json.dumps({"qrcode": None})
|
||||||
if json_str != json_str_temp:
|
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)
|
redis_client.publish("qrcode", json_str)
|
||||||
break
|
break
|
||||||
time.sleep(CRAWL_INTERVAL)
|
time.sleep(CRAWL_INTERVAL)
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[tool.poetry]
|
[tool.poetry]
|
||||||
name = "get-qrcode"
|
name = "get-qrcode"
|
||||||
version = "0.9.0"
|
version = "1.0.1"
|
||||||
description = ""
|
description = ""
|
||||||
authors = ["quantulr <35954003+quantulr@users.noreply.github.com>"]
|
authors = ["quantulr <35954003+quantulr@users.noreply.github.com>"]
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
|
Reference in New Issue
Block a user