Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on May 8, 2020. It is now read-only.
/pyppeteerPublic archive

Headless chrome/chromium automation library (unofficial port of puppeteer)

License

NotificationsYou must be signed in to change notification settings

miyakogi/pyppeteer

Repository files navigation

Pyppeteer has moved topyppeteer/pyppeteer


PyPIPyPI versionDocumentationTravis statusAppVeyor statuscodecov

Unofficial Python port ofpuppeteer JavaScript (headless)chrome/chromium browser automation library.

Installation

Pyppeteer requires python 3.6+.(experimentally supports python 3.5)

Install by pip from PyPI:

python3 -m pip install pyppeteer

Or install latest version fromgithub:

python3 -m pip install -U git+https://github.com/miyakogi/pyppeteer.git@dev

Usage

Note: When you run pyppeteer first time, it downloads a recent version of Chromium (~100MB).If you don't prefer this behavior, runpyppeteer-install command before running scripts which uses pyppeteer.

Example: open web page and take a screenshot.

importasynciofrompyppeteerimportlaunchasyncdefmain():browser=awaitlaunch()page=awaitbrowser.newPage()awaitpage.goto('http://example.com')awaitpage.screenshot({'path':'example.png'})awaitbrowser.close()asyncio.get_event_loop().run_until_complete(main())

Example: evaluate script on the page.

importasynciofrompyppeteerimportlaunchasyncdefmain():browser=awaitlaunch()page=awaitbrowser.newPage()awaitpage.goto('http://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())

Pyppeteer has almost same API as puppeteer.More APIs are listed in thedocument.

Puppeteer's documentandtroubleshooting are also useful for pyppeteer users.

Differences between puppeteer and pyppeteer

Pyppeteer is to be as similar as puppeteer, but some differences between pythonand JavaScript make it difficult.

These are differences between puppeteer and pyppeteer.

Keyword arguments for options

Puppeteer uses object (dictionary in python) for passing options tofunctions/methods. Pyppeteer accepts both dictionary and keyword arguments foroptions.

Dictionary style option (similar to puppeteer):

browser=awaitlaunch({'headless':True})

Keyword argument style option (more pythonic, isn't it?):

browser=awaitlaunch(headless=True)

Element selector method name ($ ->querySelector)

In python,$ is not usable for method name.So pyppeteer usesPage.querySelector()/Page.querySelectorAll()/Page.xpath() instead ofPage.$()/Page.$$()/Page.$x(). Pyppeteer also has shorthands for thesemethods,Page.J(),Page.JJ(), andPage.Jx().

Arguments ofPage.evaluate() andPage.querySelectorEval()

Puppeteer's version ofevaluate() takes JavaScript raw function or string ofJavaScript expression, but pyppeteer takes string of JavaScript. JavaScriptstrings can be function or expression. Pyppeteer tries to automatically detectthe string is function or expression, but sometimes it fails. If expressionstring is treated as function and error is raised, addforce_expr=True option,which force pyppeteer to treat the string as expression.

Example to get page content:

content=awaitpage.evaluate('document.body.textContent',force_expr=True)

Example to get element's inner text:

element=awaitpage.querySelector('h1')title=awaitpage.evaluate('(element) => element.textContent',element)

Future Plan

  1. Catch up development of puppeteer
    • Not intend to add original API which puppeteer does not have

Credits

This package was created withCookiecutter and theaudreyr/cookiecutter-pypackage project template.

About

Headless chrome/chromium automation library (unofficial port of puppeteer)

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp