摘要:pyppeteer绕过浏览器的检测的代码,注意需要配合低版本的浏览器,我用的是71.0.3542.0的chromium。下载链接

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

windows版本

import asyncio
from pyppeteer import launch


async def main():
    # browser = await launch(headless=False, args=["--start-maximized", "--window-size=1920,1080","--disable-infobars"])
    # browser = await launch(headless=True,executablePath="C:\\chrome-win\\local-chromium\\588429\\chrome-win32\\chrome.exe", args=["--start-maximized", "--window-size=1920,1080","--disable-infobars"])
    browser = await launch(headless=False, executablePath="C:\\chrome-win\\local-chromium\\588429\\chrome-win32\\chrome.exe", args=["--start-maximized", "--window-size=1920,1080", "--disable-infobars"])

    page = await browser.newPage()
    await page.evaluateOnNewDocument("""() => {
    delete navigator.__proto__.webdriver;
    }""")
    await page.evaluateOnNewDocument('''() = > {
    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"
                        }
                    ],
        });
    }''')

    await page.evaluateOnNewDocument('''() => {
        Object.defineProperty(navigator, 'languages', {
            get: () => ['en-US', 'en'],
        });
    }''')

    await page.evaluateOnNewDocument('''() => {
        Object.defineProperty(navigator, 'platform', {
            get: () => "Win32",
            configurable: true
        });
    }''')

    await page.evaluateOnNewDocument('''() => {
        window.chrome = {
        embeddedSearch: function() {},
        loadTimes: function() {},
        csi: function() {},
        app: {},
        webstore:{}
        };
    }''')
    await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3542.0 Safari/537.36')
    await page.setViewport({'width': 1920, 'height': 1080})
    await page.goto('https://www.aqistudy.cn/historydata/daydata.php?city=%E4%B8%8A%E6%B5%B7&month=201406')
    await asyncio.sleep(100)
    await browser.close()


asyncio.get_event_loop().run_until_complete(main())

linux版本

import asyncio
from pyppeteer import launch


async def main():
    # browser = await launch(headless=False, args=["--start-maximized", "--window-size=1920,1080","--disable-infobars"])
    # browser = await launch(headless=True,executablePath="C:\\chrome-win\\local-chromium\\588429\\chrome-win32\\chrome.exe", args=["--start-maximized", "--window-size=1920,1080","--disable-infobars"])
    browser = await launch(headless=False, executablePath="C:\\chrome-win\\local-chromium\\588429\\chrome-win32\\chrome.exe", args=["--start-maximized", "--window-size=1920,1080", "--disable-infobars"])

    page = await browser.newPage()
    await page.evaluateOnNewDocument("""() => {
    delete navigator.__proto__.webdriver;
    }""")
    await page.evaluateOnNewDocument('''() = > {
    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"
                        }
                    ],
        });
    }''')

    await page.evaluateOnNewDocument('''() => {
        Object.defineProperty(navigator, 'languages', {
            get: () => ['en-US', 'en'],
        });
    }''')

    await page.evaluateOnNewDocument('''() => {
        Object.defineProperty(navigator, 'platform', {
            get: () => "Linux x86_64",
            configurable: true
        });
    }''')

    await page.evaluateOnNewDocument('''() => {
        window.chrome = {
        embeddedSearch: function() {},
        loadTimes: function() {},
        csi: function() {},
        app: {},
        webstore:{}
        };
    }''')
    await page.setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3477.0 Safari/537.36")
    await page.setViewport({'width': 1920, 'height': 1080})
    await page.goto('https://www.aqistudy.cn/historydata/daydata.php?city=%E4%B8%8A%E6%B5%B7&month=201406')
    await asyncio.sleep(100)
    await browser.close()


asyncio.get_event_loop().run_until_complete(main())
文章目录