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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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)

  

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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

  

 

 

火狐

1
2
3
4
5
6
7
8
9
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)

  

 

posted @   CrossPython  阅读(647)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
历史上的今天:
2018-10-20 VIM编码检查
2018-10-20 mysql 远程连接
2018-10-20 CentOS7下部署Django项目详细操作步骤
2018-10-20 Django之404、500、400错误处理
点击右上角即可分享
微信分享提示