Package: pyppeteer
Attention: This repo is unmaintained and has been outside of minor changes for a long time. Please considerplaywright-pythonas an alternative.If you would like to overhaul this code to bring it up to date, please contactmepyppeteerNote: this is a continuation of thepyppeteer projectUnofficial Python port ofpuppeteerJavaScript (headless) chrome/chromium browser automation library.Free software: MIT license (including the work distributed under the Apache 2.0 license)Documentation:https://pyppeteer.github.io/pyppeteer/Installationpyppeteer requires Python >= 3.8Install withpipfrom PyPI:pip install pyppeteerOr install the latest version fromthis github repo:pip install -U git+https://github.com/pyppeteer/pyppeteer@devUsageNote: When you run pyppeteer for the first time, it downloads the latest version of Chromium (~150MB) if it is not found on your system. If you don't prefer this behavior, ensure that a suitable Chrome binary is installed. One way to do this is to runpyppeteer-installcommand before prior to using this library.Full documentation can be foundhere.Puppeteer's documentationandits troubleshooting guideare also great resources for pyppeteer users.ExamplesOpen web page and take a screenshot:importasynciofrompyppeteerimportlaunchasyncdefmain():browser=awaitlaunch()page=awaitbrowser.newPage()awaitpage.goto('https://example.com')awaitpage.screenshot({'path':'example.png'})awaitbrowser.close()asyncio.get_event_loop().run_until_complete(main())Evaluate javascript on a page:importasynciofrompyppeteerimportlaunchasyncdefmain():browser=awaitlaunch()page=awaitbrowser.newPage()awaitpage.goto('https://example.com')awaitpage.screenshot({'path':'example.png'})dimensions=awaitpage.evaluate('''() => {return {width: document.documentElement.clientWidth,height: document.documentElement.clientHeight,deviceScaleFactor: window.devicePixelRatio,}}''')print(dimensions)# >>> {'width': 800, 'height': 600, 'deviceScaleFactor': 1}awaitbrowser.close()asyncio.get_event_loop().run_until_complete(main())Differences between puppeteer and pyppeteerpyppeteer strives to replicate the puppeteer API as close as possible, however, fundamental differences between Javascript and Python make this difficult to do precisely. More information on specifics can be found in thedocumentation.Keyword arguments for optionspuppeteer uses an object for passing options to functions/methods. pyppeteer methods/functions accept both dictionary (python equivalent to JavaScript's objects) and keyword arguments for options.Dictionary style options (similar to puppeteer):browser=awaitlaunch({'headless':True})Keyword argument style options (more pythonic, isn't it?):browser=awaitlaunch(headless=True)Element selector method namesIn python,$is not a valid identifier. The equivalent methods to Puppeteer's$,$$, and$xmethods are listed below, along with some shorthand methods for your convenience:puppeteerpyppeteerpyppeteer shorthandPage.$()Page.querySelector()Page.J()Page.$$()Page.querySelectorAll()Page.JJ()Page.$x()Page.xpath()Page.Jx()Arguments ofPage.evaluate()andPage.querySelectorEval()puppeteer's version ofevaluate()takes a JavaScript function or a string representation of a JavaScript expression. pyppeteer takes string representation of JavaScript expression or function. pyppeteer will try to automatically detect if the string is function or expression, but it will fail sometimes. If an expression is erroneously treated as function and an error is raised, try settingforce_exprtoTrue, to force pyppeteer to treat the string as expression.Examples:Get a page'stextContent:content=awaitpage.evaluate('document.body.textContent',force_expr=True)Get an element'stextContent:element=awaitpage.querySelector('h1')title=awaitpage.evaluate('(element) => element.textContent',element)RoadmapSeeprojectsCreditsThis package was created withCookiecutterand theaudreyr/cookiecutter-pypackageproject template.