1. 引用
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
2.Option设置
options = Options()
#禁用gpu加速
options.add_argument('--disable-gpu')
#无头模式
# options.add_argument("--headless")
#设置user-agent
# options.add_argument('user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36')
# 隐藏 正在受到自动软件的控制 这几个字
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
# 更改浏览器中网页打印选项为保存为pdf,并且更改保存文件夹
appState = {
'recentDestinations': [
{
'id': 'Save as PDF',
'origin': 'local',#本地保存
'account': ''#不与任何账户关联
}
],
'selectedDestinationId': 'Save as PDF',
'version': 2,
#设置打印尺寸
"mediaSize": {
"height_microns": 420000,
"imageable_area_bottom_microns": 0,
"imageable_area_left_microns": 0,
"imageable_area_right_microns": 297000,
"imageable_area_top_microns": 420000,
"name": "ISO_A3",
"width_microns": 297000,
"custom_display_name": "A3"
}
}
prefs = {
'printing.print_preview_sticky_settings.appState': json.dumps(appState),
'savefile.default_directory': pdf_savepath #文件保存路径
}
options.add_experimental_option('prefs', prefs)
# 设置弹出打印窗口之后自动点确定
options.add_argument('--kiosk-printing')
3.初始化driver
service = webdriver.ChromeService(executable_path=driver_path)
driver = webdriver.Chrome(options=options, service=service)
4.避免网站检测是driver控制
with open('stealth.min.js') as f:
js = f.read()
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
"source": js
})