python-selenium-spider/main.py
2024-03-01 17:14:56 +08:00

38 lines
1.4 KiB
Python

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import time
import utils
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
# })
# """
# })
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)