121 lines
4.4 KiB
Python
121 lines
4.4 KiB
Python
import configparser
|
||
import logging
|
||
import os
|
||
from time import sleep
|
||
from selenium.webdriver.common.by import By
|
||
from selenium.webdriver.support.ui import WebDriverWait
|
||
from selenium.webdriver.support import expected_conditions as EC
|
||
from selenium import webdriver
|
||
from selenium.webdriver.chrome.options import Options
|
||
from selenium.webdriver.chrome.service import Service
|
||
from webdriver_manager.chrome import ChromeDriverManager
|
||
from webdriver_manager.microsoft import EdgeChromiumDriverManager
|
||
from webdriver_manager.firefox import GeckoDriverManager
|
||
import ssl
|
||
os.environ['WDM_SSL_VERIFY'] = "false"
|
||
|
||
# 配置日志
|
||
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
||
|
||
# 读取配置文件
|
||
config = configparser.ConfigParser()
|
||
config.read('config.ini', encoding="utf-8")
|
||
|
||
# 从配置文件中读取参数
|
||
url_base = config.get('base', 'url_base')
|
||
url_login = r"https://mozi-login.alibaba-inc.com/?APP_NAME=LPD_TEAM_AEOLUS&BACK_URL=https%3A%2F%2Faeolus.ele.me"
|
||
url_home = config.get('base', 'url_home')
|
||
url_work = config.get('base', 'url_work')
|
||
account = config.get('base', 'account')
|
||
password = config.get('base', 'password')
|
||
interval = int(config.get('base', 'interval'))
|
||
driver_type = config.get('base', 'driver')
|
||
driver_path_edge = config.get('base', 'driver_path_edge')
|
||
driver_path_chrome = config.get('base', 'driver_path_chrome')
|
||
# WebDriver初始化
|
||
def init_webdriver(driver_type):
|
||
options = Options()
|
||
manager = {
|
||
"firefox": GeckoDriverManager,
|
||
"edge": EdgeChromiumDriverManager,
|
||
"chrome": ChromeDriverManager
|
||
}[driver_type.lower()]
|
||
|
||
driver_path = None # 初始化为None,以防安装失败
|
||
try:
|
||
driver_path = manager().install()
|
||
logging.info("找到驱动" + driver_path)
|
||
service = Service(executable_path=driver_path)
|
||
|
||
if driver_type.lower() == "firefox":
|
||
oprofile = webdriver.FirefoxOptions()
|
||
oprofile.accept_insecure_certs = True
|
||
driver = webdriver.Firefox(service=service, options=oprofile)
|
||
elif driver_type.lower() == "edge":
|
||
driver = webdriver.Edge(service=service, options=options)
|
||
else: # chrome
|
||
options.add_argument('--ignore-certificate-errors')
|
||
driver = webdriver.Chrome(service=service, options=options)
|
||
|
||
logging.info(f"成功 - 使用{driver_type}")
|
||
return driver
|
||
|
||
except Exception as e:
|
||
logging.error(f"无法创建WebDriver实例: {e}")
|
||
if driver_type.lower() == "edge" and driver_path_edge: # 只有在Edge类型且路径已知的情况下才尝试使用已存在的驱动
|
||
edge_options = Options() # 创建Edge的Options实例
|
||
service = Service(executable_path=driver_path_edge) # 使用已知的驱动路径
|
||
driver = webdriver.Edge(service=service, options=edge_options) # 传入Options实例
|
||
logging.info("使用自行安装的Edge")
|
||
return driver
|
||
else:
|
||
edge_options = Options() # 创建Edge的Options实例
|
||
service = Service(executable_path=driver_path_chrome) # 使用已知的驱动路径
|
||
driver = webdriver.Edge(service=service, options=edge_options) # 传入Options实例
|
||
logging.info("使用自行安装的Chrome")
|
||
return driver
|
||
|
||
# 登录流程
|
||
def login(driver, url_login, account, password):
|
||
driver.get(url_login)
|
||
WebDriverWait(driver, 10).until(EC.url_to_be(url_login))
|
||
|
||
username_field = driver.find_element(By.NAME, 'domainAccount')
|
||
username_field.clear()
|
||
username_field.send_keys(account)
|
||
|
||
password_field = driver.find_element(By.NAME, 'password')
|
||
password_field.clear()
|
||
password_field.send_keys(password)
|
||
|
||
login_button = WebDriverWait(driver, 10).until(
|
||
EC.element_to_be_clickable((By.CLASS_NAME, 'sso-btn-submit'))
|
||
)
|
||
login_button.click()
|
||
|
||
WebDriverWait(driver, 10).until_not(EC.url_to_be(url_login))
|
||
logging.info("登录成功!")
|
||
|
||
|
||
# 主要逻辑
|
||
def main():
|
||
driver = init_webdriver(driver_type)
|
||
if driver is None:
|
||
logging.error("")
|
||
return "驱动异常"
|
||
|
||
try:
|
||
login(driver, url_login, account, password)
|
||
driver.get(url_home)
|
||
driver.get(url_work)
|
||
|
||
while driver.current_url == url_work:
|
||
sleep(interval)
|
||
# 你的数据抓取逻辑
|
||
pass
|
||
finally:
|
||
driver.quit()
|
||
|
||
|
||
if __name__ == '__main__':
|
||
main() |