This commit is contained in:
quantulr
2024-01-15 17:19:41 +08:00
parent ea8591db15
commit aef8e6080e
2 changed files with 43 additions and 30 deletions

View File

@ -1,38 +1,68 @@
import base64 import base64
import json import json
import logging
import time import time
import redis import redis
from selenium import webdriver from selenium import webdriver
from selenium.common import NoSuchElementException, NoSuchWindowException from selenium.common import NoSuchElementException, NoSuchWindowException, UnexpectedAlertPresentException
from selenium.webdriver.common.by import By 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
def get_qrcode(): def get_qrcode():
driver = webdriver.Chrome() service = webdriver.ChromeService(executable_path="C:/Users/Administrator/Documents/chromedriver_win32/chromedriver.exe")
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("http://localhost:3030") driver.get("https://icme.haoyisheng.com")
json_str_temp = "" json_str_temp = ""
while True: while True:
try: try:
qrcode_canvas = driver.find_element(By.CLASS_NAME, "qrcode") qrcode_tab = driver.find_element(By.CSS_SELECTOR, ".x-tab-strip-active .x-tab-strip-text")
qrcode_base64 = driver.execute_script("return arguments[0].toDataURL('image/png').substring(22);", if qrcode_tab.get_attribute("innerHTML") != "课题二维码":
qrcode_canvas) json_str = json.dumps({"qrcode": None})
# qrcode_png = base64.b64decode(qrcode_base64) if json_str != json_str_temp:
json_str = json.dumps({"qrcode": qrcode_base64}) json_str_temp = json_str
redis_client.publish("qrcode", json_str)
print("未打开二维码页面")
time.sleep(CRAWL_INTERVAL)
continue
# 查找二维码所在iframe
qrcode_frame = driver.find_element(By.CSS_SELECTOR, "#menuItemSubjectQrCode iframe")
# 切换到iframe
driver.switch_to.frame(qrcode_frame)
# 获取二维码src
qrcode_img = driver.find_element(By.CSS_SELECTOR, "#imgDiv #img")
qrcode_src = qrcode_img.get_attribute("src")
# 课题名称,起止时间
topic_title = driver.find_element(By.CSS_SELECTOR, "#title h1:first-child").text
start_time = driver.find_element(By.CSS_SELECTOR, "#title #p1").text
end_time = driver.find_element(By.CSS_SELECTOR, "#title #p2").text
json_str = json.dumps(
{"qrcode": qrcode_src, "title": topic_title, "start_time": start_time, "end_time": end_time})
if json_str != json_str_temp: if json_str != json_str_temp:
json_str_temp = json_str json_str_temp = json_str
redis_client.publish("qrcode", json_str) redis_client.publish("qrcode", json_str)
print("二维码已更新") print("二维码已更新", topic_title, start_time, end_time)
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:
json_str_temp = json_str json_str_temp = json_str
redis_client.publish("qrcode", json_str) redis_client.publish("qrcode", json_str)
driver.switch_to.default_content()
print("二维码不存在") print("二维码不存在")
except UnexpectedAlertPresentException:
print("UnexpectedAlertPresentException")
time.sleep(CRAWL_INTERVAL)
continue
except NoSuchWindowException: except NoSuchWindowException:
print("窗口已关闭") print("窗口已关闭")
json_str = json.dumps({"qrcode": None}) json_str = json.dumps({"qrcode": None})
@ -40,29 +70,12 @@ def get_qrcode():
json_str_temp = json_str json_str_temp = json_str
redis_client.publish("qrcode", json_str) redis_client.publish("qrcode", json_str)
break break
time.sleep(0.5) time.sleep(CRAWL_INTERVAL)
driver.quit() driver.quit()
def main(): def main():
get_qrcode() get_qrcode()
# driver = webdriver.Chrome()
# driver.get("http://localhost:3030")
# # qrcode_login = driver.find_element(By.ID, "lbApp")
# # qrcode_login.click()
# # qrcode_canvas = None
#
# qrcode_canvas = WebDriverWait(driver, 1000 * 60 * 60 * 24).until(
# expected_conditions.presence_of_element_located((By.CSS_SELECTOR, ".qrcode"))
# )
# qrcode_base64 = driver.execute_script("return arguments[0].toDataURL('image/png').substring(22);", qrcode_canvas)
#
# qrcode_png = base64.b64decode(qrcode_base64)
#
# with open("qrcode.png", "wb") as qrcode_png_file:
# qrcode_png_file.write(qrcode_png)
# # time.sleep(8)
# driver.quit()
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -1,6 +1,6 @@
[tool.poetry] [tool.poetry]
name = "get-qrcode" name = "get-qrcode"
version = "0.1.0" version = "0.9.0"
description = "" description = ""
authors = ["quantulr <35954003+quantulr@users.noreply.github.com>"] authors = ["quantulr <35954003+quantulr@users.noreply.github.com>"]
readme = "README.md" readme = "README.md"
@ -17,4 +17,4 @@ requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api" build-backend = "poetry.core.masonry.api"
[tool.poetry.scripts] [tool.poetry.scripts]
dev = "get_qrcode.main:main" get-qrcode = "get_qrcode.main:main"