From c3c1e6ccc4035c26c47efd58b76ca8d425350fe9 Mon Sep 17 00:00:00 2001 From: shikong <919411476@qq.com> Date: Sat, 2 Mar 2024 03:21:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95=20selenium=20=E9=99=84?= =?UTF-8?q?=E5=8A=A0=E5=88=B0=E6=B5=8F=E8=A7=88=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 61 ++++++++++++++++++++++------------------------ main_subprocess.py | 27 ++++++++++++++++++++ run/__init__.py | 15 ++++++++++++ 3 files changed, 71 insertions(+), 32 deletions(-) create mode 100644 main_subprocess.py create mode 100644 run/__init__.py diff --git a/main.py b/main.py index 11268ef..7bbe588 100644 --- a/main.py +++ b/main.py @@ -1,38 +1,35 @@ from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait import time +import run -import utils +if __name__ == '__main__': + options = webdriver.ChromeOptions() + options.add_argument('lang=zh-CN') + options.add_argument( + 'user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36') + # options.add_argument('--user-data-dir=./UserData/Google/Chrome/Default') + options.add_argument('--disable-gpu') # 如果不加这个选项,有时定位会出现问题 + # options.add_argument('--headless') # 增加无界面选项 -options = webdriver.ChromeOptions() -options.add_argument('lang=zh-CN') -options.add_argument( - 'user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36') -# options.add_argument('--user-data-dir=./UserData/Google/Chrome/Default') -options.add_argument('--disable-gpu') # 如果不加这个选项,有时定位会出现问题 -# options.add_argument('--headless') # 增加无界面选项 + service = webdriver.ChromeService(r"./driver/chromedriver.exe") + driver = webdriver.Chrome(service=service,options=options) + # driver.maximize_window() + # driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", { + # "source": """ + # Object.defineProperty(navigator, 'webdriver', { + # get: () => undefined + # }) + # """ + # }) + + try: + with open("./stealth.min.js") as f: + driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", { + "source": f.read() + }) -service = webdriver.ChromeService(r"./driver/chromedriver.exe") -driver = webdriver.Chrome(service=service,options=options) -driver.maximize_window() -# driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", { -# "source": """ -# Object.defineProperty(navigator, 'webdriver', { -# get: () => undefined -# }) -# """ -# }) -with open("./stealth.min.js") as f: - driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", { - "source": f.read() - }) - -driver.get("https://bot.sannysoft.com/") -time.sleep(5) -width = driver.execute_script("return document.documentElement.scrollWidth") -height = driver.execute_script("return document.documentElement.scrollHeight") -print(width, height) -utils.screenshot(driver=driver, save_path="./screenshot.png") -driver.set_window_size(width, height) -driver.save_screenshot('result.png') -time.sleep(30) \ No newline at end of file + run.run(driver=driver) + time.sleep(5) + finally: + driver.quit() \ No newline at end of file diff --git a/main_subprocess.py b/main_subprocess.py new file mode 100644 index 0000000..c2101a0 --- /dev/null +++ b/main_subprocess.py @@ -0,0 +1,27 @@ +import subprocess +from selenium import webdriver +from selenium.webdriver.chrome.options import Options +import time +import run +import utils + +# 通过 已打开的浏览器 附加 selenium + +if __name__ == '__main__': + subprocess.Popen([ + "C:\Program Files\Google\Chrome\Application\chrome.exe", + # "--disable-gpu", + "--remote-debugging-port=9222", + r'--user-data-dir=E:\Repository\skcks.cn\python-selenium-spider\tmp\UserData']) + + # service = webdriver.ChromeService(r"./driver/chromedriver.exe") + chrome_options = Options() + chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222") + driver = webdriver.Chrome(options=chrome_options) + + try: + run.run(driver=driver) + time.sleep(5) + finally: + driver.close() + driver.quit() \ No newline at end of file diff --git a/run/__init__.py b/run/__init__.py new file mode 100644 index 0000000..1054a87 --- /dev/null +++ b/run/__init__.py @@ -0,0 +1,15 @@ +from selenium import webdriver +from selenium.webdriver.support.ui import WebDriverWait +import time +from selenium.webdriver.remote.webdriver import WebDriver + +import utils + + +def run(driver:WebDriver): + driver.set_window_size(1920, 1080) + driver.get("https://bot.sannysoft.com/") + width = driver.execute_script("return document.documentElement.scrollWidth") + height = driver.execute_script("return document.documentElement.scrollHeight") + print(width, height) + utils.screenshot(driver=driver, save_path="./screenshot.png") \ No newline at end of file