1. 程式人生 > 實用技巧 >puppeteer 無頭瀏覽器防檢測

puppeteer 無頭瀏覽器防檢測

因業務需求封裝成了方法,直接把方法中的語句複製貼上即可

async function setBrowserPage(page){
    // 設定user_agent
    await page.setUserAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4181.9 Safari/537.36")
    // 設定webdriver
    await page.evaluate(() => {
        Object.defineProperty(navigator, 
'webdriver', { get: () => false }); 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" } ], }); }); }