摘要:selenium绕过浏览器的检测的代码,注意需要配合低版本的浏览器,我用的是71.0.3542.0的chromium。下载链接,以及71.0.3578.80版本的chromedriver,chromedriver需要经过特殊的反编译处理。

windows版本和linux版本区别在于UA头的不同以及platform的不同。

windows版本

from selenium import webdriver

chrome_options = webdriver.ChromeOptions()

# 开启无界面模式
# chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--disable-extensions')
chrome_options.add_argument('--profile-directory=Default')
chrome_options.add_argument("--incognito")
chrome_options.add_argument("--disable-plugins-discovery")
chrome_options.add_argument("--start-maximized")
chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])
chrome_options.add_experimental_option('useAutomationExtension', False)
chrome_options.add_argument('disable-infobars')
chrome_options.add_argument("--disable-blink-features=AutomationControlled")

driver = webdriver.Chrome(options=chrome_options)

driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
  "source": """
        delete navigator.__proto__.webdriver;
  """
})

driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
  "source": """
        Object.defineProperty(navigator, 'plugins', {
            get: () = > [
                            {
                                0: {type: "application/x-google-chrome-pdf", suffixes: "pdf",
                                    description: "Portable Document Format", enabledPlugin: Plugin},
                                description: "Portable Document Format",
                                filename: "internal-pdf-viewer",
                                length: 1,
                                name: "Chrome PDF Plugin"
                            },
                            {
                                0: {type: "application/pdf", suffixes: "pdf", description: "", enabledPlugin: Plugin},
                                description: "",
                                filename: "mhjfbmdgcfjbbpaeojofohoefgiehjai",
                                length: 1,
                                name: "Chrome PDF Viewer"
                            },
                            {
                                0: {type: "application/x-nacl", suffixes: "", description: "Native Client Executable",
                                    enabledPlugin: Plugin},
                                1: {type: "application/x-pnacl", suffixes: "",
                                    description: "Portable Native Client Executable", enabledPlugin: Plugin},
                                description: "",
                                filename: "internal-nacl-plugin",
                                length: 2,
                                name: "Native Client"
                            }
                        ],
            });
  """
})

driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
  "source": """
        Object.defineProperty(navigator, 'languages', {
            get: () => ['en-US', 'en'],
        });
  """
})

driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
  "source": """
        Object.defineProperty(navigator, 'platform', {
                    get: () => "Win32",
                    configurable: true
        });
  """
})

driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
  "source": """
        window.chrome = {
        embeddedSearch: function() {},
        loadTimes: function() {},
        csi: function() {},
        app: {},
        webstore:{}
        };
  """
})


driver.execute_cdp_cmd('Network.setUserAgentOverride', {"userAgent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.53 Safari/537.36'})


driver.get('https://www.aqistudy.cn/historydata/daydata.php?city=%E4%B8%8A%E6%B5%B7&month=201406')
driver.implicitly_wait(10)

driver.get_screenshot_as_file("截屏天气网1.png")
f = open("天气网界面1.html", "w", encoding="utf-8")
content = driver.page_source
f.write(content)
f.close()

f = open("天气网界面2.html", "w", encoding="utf-8")
content = driver.execute_script("return document.documentElement.outerHTML")
f.write(content)
f.close()

linux版本

from selenium import webdriver

chrome_options = webdriver.ChromeOptions()

# 开启无界面模式
# chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--disable-extensions')
chrome_options.add_argument('--profile-directory=Default')
chrome_options.add_argument("--incognito")
chrome_options.add_argument("--disable-plugins-discovery")
chrome_options.add_argument("--start-maximized")
chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])
chrome_options.add_experimental_option('useAutomationExtension', False)
chrome_options.add_argument('disable-infobars')
chrome_options.add_argument("--disable-blink-features=AutomationControlled")

driver = webdriver.Chrome(options=chrome_options)

driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
  "source": """
        delete navigator.__proto__.webdriver;
  """
})

driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
  "source": """
        Object.defineProperty(navigator, 'plugins', {
            get: () = > [
                            {
                                0: {type: "application/x-google-chrome-pdf", suffixes: "pdf",
                                    description: "Portable Document Format", enabledPlugin: Plugin},
                                description: "Portable Document Format",
                                filename: "internal-pdf-viewer",
                                length: 1,
                                name: "Chrome PDF Plugin"
                            },
                            {
                                0: {type: "application/pdf", suffixes: "pdf", description: "", enabledPlugin: Plugin},
                                description: "",
                                filename: "mhjfbmdgcfjbbpaeojofohoefgiehjai",
                                length: 1,
                                name: "Chrome PDF Viewer"
                            },
                            {
                                0: {type: "application/x-nacl", suffixes: "", description: "Native Client Executable",
                                    enabledPlugin: Plugin},
                                1: {type: "application/x-pnacl", suffixes: "",
                                    description: "Portable Native Client Executable", enabledPlugin: Plugin},
                                description: "",
                                filename: "internal-nacl-plugin",
                                length: 2,
                                name: "Native Client"
                            }
                        ],
            });
  """
})

driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
  "source": """
        Object.defineProperty(navigator, 'languages', {
            get: () => ['en-US', 'en'],
        });
  """
})

driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
  "source": """
        Object.defineProperty(navigator, 'platform', {
                    get: () => "Linux x86_64",
                    configurable: true
        });
  """
})

driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
  "source": """
        window.chrome = {
        embeddedSearch: function() {},
        loadTimes: function() {},
        csi: function() {},
        app: {},
        webstore:{}
        };
  """
})


driver.execute_cdp_cmd('Network.setUserAgentOverride', {"userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3477.0 Safari/537.36"})


driver.get('https://www.aqistudy.cn/historydata/daydata.php?city=%E4%B8%8A%E6%B5%B7&month=201406')
driver.implicitly_wait(10)

driver.get_screenshot_as_file("截屏天气网1.png")
f = open("天气网界面1.html", "w", encoding="utf-8")
content = driver.page_source
f.write(content)
f.close()

f = open("天气网界面2.html", "w", encoding="utf-8")
content = driver.execute_script("return document.documentElement.outerHTML")
f.write(content)
f.close()
文章目录