selenium无头浏览器,禁用图片,禁用js,切换UA,反爬

发布时间 2023-10-20 13:52:04作者: CrossPython
from selenium import webdriver
from fake_useragent import UserAgent

ua = UserAgent().random
options = webdriver.ChromeOptions()
options.add_argument('--no-sandbox')  # 停用沙箱
options.add_argument('--disable-gpu')  # 禁用GPU实现加速
options.add_argument('--ignore-certificate-errors')  # 忽略证书错误
options.add_argument('–hide-scrollbars') # 隐藏滚动条, 应对一些特殊页面
options.add_experimental_option('excludeSwitches', ['enable-automation']) # 禁用浏览器正在被自动化程序控制的提示
options.add_argument('–incognito') # 隐身模式(无痕模式)
options.add_argument('--disable-dev-shm-usage')
# 修改User-Agent, 无头和正常的UA是不一样的
options.add_argument('user-agent=' + ua)
# 这种方式在非无头Headless模式下是生效的
# prefs = {
# 'profile.default_content_settings': {
# 'profile.default_content_setting_values': {
# 'images': 2, # 不加载图片
# 'javascript': 2, # 不加载JS
# "User-Agent": ua, # 更换UA
# }}}
# options.add_experimental_option("prefs", prefs)

# 这种方式在无头Headless模式下是生效的, 非无头Headless模式下也是生效的。
options.add_argument('blink-settings=imagesEnabled=false')

# 添加代理
# options.add_argument(f"--proxy-server={ip}:port")     #不需要http://,只保留ip和端口号

# 无界面浏览器
options.add_argument('--window-size=1920,1080')
options.add_argument('--headless')

# 隐藏滚动条, 应对一些特殊页面
options.add_argument('--hide-scrollbars')

# 设备名称
mobileEmulation = {'deviceName': 'iPhone X'}
options.add_experimental_option('mobileEmulation', mobileEmulation)

# [如何正确移除Selenium中的 window.navigator.webdriver](https://zhuanlan.zhihu.com/p/117506307)
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
# 初始化实例
browser = webdriver.Chrome(executable_path="C:/chrome/chromedriver.exe", options=options)

browser.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
    "source": """
    Object.defineProperty(navigator, 'webdriver', {
      get: () => undefined
    })
  """
})

browser.maximize_window()
# 这个网页可以显示浏览器的信息,这样我们就可以看到我们的UA信息,
url = "https://httpbin.org/get?show_env=1"
browser.get(url)

  

 

def get_driver():
    options = webdriver.ChromeOptions()
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-dev-shm-usage')
    options.add_argument('user-agent=' + 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36')
    options.add_argument('--window-size=1920,1080')
    options.add_argument('--headless')
    options.add_argument(f'--ignore-certificate-errors')
    options.add_argument('--disable-gpu')
    options.add_argument('--hide-scrollbars')
    mobileEmulation = {'deviceName': 'iPhone X'}
    options.add_experimental_option('mobileEmulation', mobileEmulation)
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    driver = webdriver.Chrome(executable_path="./../bin/chromedriver.exe", options=options)

    driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
        "source": """
        Object.defineProperty(navigator, 'webdriver', {
          get: () => undefined
        })
      """
    })
    driver.maximize_window()
    wait = driver.implicitly_wait(10)
    wait = WebDriverWait(driver, 10)
    return driver, wait 

  

 

 

火狐

firefox_options = webdriver.FirefoxOptions()
firefox_options.add_argument('--headless')  # 无头
firefox_options.add_argument('--disable-gpu')  # 避免bug
firefox_options.set_preference('permissions.default.image', 2)  # 禁用图片
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.http', IP)  # IP为你的代理服务器地址:如‘127.0.0.0’,字符串类型
profile.set_preference('network.proxy.http_port', PORT)  # PORT为代理服务器端口号:如,9999,整数类型
browser = webdriver.Firefox(firefox_options=firefox_options)
browser.get(response.url)