from pyppeteer import launch
import asyncio
from loguru import logger
BROWSER_WIDTH = 1920
BROWSER_HEIGHT = 1080
OPTIONS = {
'headless': False,
'slowMo': 1.3,
'args': [
'--start-maximized',
'--enable-automation',
'--disable-extensions',
'--hide-scrollbars',
'--disable-bundled-ppapi-flash',
'--mute-audio',
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-gpu',
'--disable-infobars'
],
'dumpio': True
}
async def page_evaluate(page, width=BROWSER_WIDTH, height=BROWSER_HEIGHT):
await page.setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36")
await page.setViewport({
'width': width, 'height': height})
await page.evaluate('''() =>{ Object.defineProperties(navigator,{ webdriver:{ get: () => false } });window.screen.width=1366; }''')
await page.evaluate('''() =>{ window.navigator.chrome = { runtime: {}, };}''')
await page.evaluate('''() =>{ Object.defineProperty(navigator, 'languages', { get: () => ['en-US', 'en'] }); }''')
await page.evaluate('''() =>{ Object.defineProperty(navigator, 'plugins', { get: () => [1, 2, 3, 4, 5,6], }); }''')
await page.evaluateOnNewDocument("""() => {Object.defineProperty(navigator,'webdriver',{get:() => undefined})}""")
logger.success("浏览器初始化成功")
async def main():
browser = await launch(OPTIONS)
page = await browser.newPage()
await page_evaluate(page)
await page.goto("https://www.baidu.com/")
await asyncio.sleep(3)
print(await page.content())
await asyncio.sleep(5000)
await browser.close()
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())