Ubuntu无界面Linux 使用pyppeteer 报错:pyppeteer.errors.BrowserError: Browser closed unexpectedly:

 安装依赖

sudo apt-get install  gconf-service libasound2 libatk1.0-0 libatk-bridge2.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget
browser=await launch(options={'args': ['--no-sandbox']})
import asyncio
from pyppeteer import launch
import time

async def page_evaluate(page):
    # 替换淘宝在检测浏览时采集的一些参数
    # 需要注意,在测试的过程中发现登陆成功后页面的该属性又会变成True
    # 所以在每次重新加载页面后要重新设置该属性的值。
    await page.evaluate('''() =>{ Object.defineProperties(navigator,{ webdriver:{ get: () => false } }) }''')
    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], }); }''')

async def main():
    #browser = await launch({
    #    'headless': False,
    #})

    browser = await launch(options={'args': ['--no-sandbox']})
    #browser = await launch()
    page = await browser.newPage()
    await page.setViewport({'width': 1366, 'height': 768})
    res = await page.goto('https://www.qcc.com')
    resp_headers = res.headers  # 响应头
    resp_status = res.status  # 响应状态
    print('响应头:',resp_headers)
    print('响应状态:',resp_status)

    await page_evaluate(page)

    # time.sleep(100)
    await browser.close()

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

 

你可能感兴趣的:(Python,Linux)