Python全系列 教程
3567个小节阅读:5931.9k
目录
鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
chrome59版本以后可以变成无头的浏览器,加以下参数
xxxxxxxxxx
def test_headless():
# 设置参数,将浏览器隐藏起来(无头浏览器)
options = ChromeOptions()
options.add_argument('--headless')
# 设置驱动
service = Service('./chromedriver')
# 启动Chrome浏览器
driver = Chrome(service=service,options=options)
# 访问页面
driver.get('https://www.baidu.com')
# 打印代码
print(driver.page_source)
# 关闭浏览器
driver.quit()
代理模式
xxxxxxxxxx
def test_proxy1():
# 设置参数,给浏览器设置代理
options = ChromeOptions()
# options.add_argument('--proxy-server=http://ip:port')
options.add_argument('--proxy-server=http://221.199.36.122:35414')
# 设置驱动
service = Service('./chromedriver')
# 启动Chrome浏览器
driver = Chrome(service=service,options=options)
# 访问页面 "134.195.101.16",
driver.get('http://httpbin.org/get')
# 打印代码
print(driver.page_source)
# 关闭浏览器
driver.quit()
def test_proxy2():
from selenium.webdriver.common.proxy import ProxyType,Proxy
# 设置参数,给浏览器设置代理
ip = 'http://113.76.133.238:35680'
proxy = Proxy()
proxy.proxy_type = ProxyType.MANUAL
proxy.http_proxy = ip
proxy.ssl_proxy = ip
# 关联浏览器
capabilities = DesiredCapabilities.CHROME
proxy.add_to_capabilities(capabilities)
# 设置驱动
service = Service('./chromedriver')
# 启动Chrome浏览器
driver = Chrome(service=service,desired_capabilities=capabilities)
# 访问页面 "134.195.101.16",
driver.get('http://httpbin.org/get')
# 打印代码
print(driver.page_source)
# 关闭浏览器
driver.quit()
防检测设置
xxxxxxxxxx
from selenium.webdriver import Chrome
from selenium.webdriver import ChromeOptions
options = ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-automation'])
options.add_experimental_option('useAutomationExtension', False)
chrome = Chrome(chrome_options=options)
chrome.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
"source": """
Object.defineProperty(navigator, 'webdriver', {
get: () => false
})
"""
})
chrome.get('http://httpbin.org/get')
info = chrome.page_source
print(info)
sleep(20)
使用
window.navigator.webdriver
检测
实时效果反馈
1. 关于selenium,参数的使用说法错误的是?
A selenium通过add_argument()增加参数
B selenium通过--headless
参数设置无头
C selenium通过--proxy-server
参数设置代理
D selenium通过--navigator
参数设置防检测
答案
1=>D