python使用pyppeteer反反屏蔽繞過WebDriver檢測

其中最主要的是添加下面的代碼:

await page.evaluateOnNewDocument('() =>{ Object.defineProperties(navigator,'
                                     '{ webdriver:{ get: () => false } }) }')

下面的是完整的代碼,可以檢測是否成功

import asyncio
from pyppeteer import launch

# 測試檢測webdriver
async def main():
    browser = await launch(headless=False, args=['--disable-infobars'])
    page = await browser.newPage()
    # 下面的主要是為了偽裝下,很多會進行屏蔽
    await page.evaluateOnNewDocument('() =>{ Object.defineProperties(navigator,'
                                     '{ webdriver:{ get: () => false } }) }')
    # 上面的主要是為了偽裝下,很多會進行屏蔽
    await page.setUserAgent("Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)")
    await page.setViewport(viewport={'width': 1920, 'height': 1080})
    await page.goto('https://intoli.com/blog/not-possible-to-block-chrome-headless/chrome-headless-test.html')
    print(await page.content())
    await asyncio.sleep(25)
    await browser.close()
# asyncio.get_event_loop().run_until_complete(main())
if __name__ == "__main__":
    asyncio.run(main())

運行之後的結果為

python使用pyppeteer反反屏蔽繞過WebDriver檢測

到此為止。

發布者:彬彬筆記,轉載請註明出處:https://www.binbinbiji.com/zh-hant/python/2344.html

(0)
彬彬筆記彬彬筆記
上一篇 2022年6月6日 21:39
下一篇 2022年6月7日

相關推薦

發表回復

登錄後才能評論
蜀ICP備14017386號-13