Skip to main content

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

Project description

Pyppeteer

this is my own temporary fork that bumps default chrome revision and handles newer chrome revisions correctly on windows

Unofficial Python port of puppeteer 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_fork

Or install latest version from github:

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

Usage

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

Example: open web page and take a screenshot.

import asyncio
from pyppeteer_fork import launch

async def main():
    browser = await launch()
    page = await browser.newPage()
    await page.goto('http://example.com')
    await page.screenshot({'path': 'example.png'})
    await browser.close()

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

Example: evaluate script on the page.

import asyncio
from pyppeteer_fork import launch

async def main():
    browser = await launch()
    page = await browser.newPage()
    await page.goto('http://example.com')
    await page.screenshot({'path': 'example.png'})

    dimensions = await page.evaluate('''() => {
        return {
            width: document.documentElement.clientWidth,
            height: document.documentElement.clientHeight,
            deviceScaleFactor: window.devicePixelRatio,
        }
    }''')

    print(dimensions)
    # >>> {'width': 800, 'height': 600, 'deviceScaleFactor': 1}
    await browser.close()

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

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

Puppeteer's document and troubleshooting are also useful for pyppeteer_fork users.

Differences between puppeteer and pyppeteer_fork

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

These are differences between puppeteer and pyppeteer_fork.

Keyword arguments for options

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

Dictionary style option (similar to puppeteer):

browser = await launch({'headless': True})

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

browser = await launch(headless=True)

Element selector method name ($ -> querySelector)

In python, $ is not usable for method name. So pyppeteer_fork uses Page.querySelector()/Page.querySelectorAll()/Page.xpath() instead of Page.$()/Page.$$()/Page.$x(). Pyppeteer also has shorthands for these methods, Page.J(), Page.JJ(), and Page.Jx().

Arguments of Page.evaluate() and Page.querySelectorEval()

Puppeteer's version of evaluate() takes JavaScript raw function or string of JavaScript expression, but pyppeteer_fork takes string of JavaScript. JavaScript strings can be function or expression. Pyppeteer tries to automatically detect the string is function or expression, but sometimes it fails. If expression string is treated as function and error is raised, add force_expr=True option, which force pyppeteer_fork to treat the string as expression.

Example to get page content:

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

Example to get element's inner text:

element = await page.querySelector('h1')
title = await page.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 with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyppeteer_fork-0.0.27.tar.gz (1.2 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyppeteer_fork-0.0.27-py2.py3-none-any.whl (80.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file pyppeteer_fork-0.0.27.tar.gz.

File metadata

  • Download URL: pyppeteer_fork-0.0.27.tar.gz
  • Upload date:
  • Size: 1.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.0.0 pkginfo/1.5.0.1 requests/2.20.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.7.3

File hashes

Hashes for pyppeteer_fork-0.0.27.tar.gz
Algorithm Hash digest
SHA256 f9d87a7262331012da62864e2a7b1a29e9a1b570398355020ff2ed85619f85a6
MD5 dce27e9c4be08038044ee1f5d2f6b27b
BLAKE2b-256 e766486bb2778c25cb94b9b79dec1da30cf5e4e9af0f7513b3c191971b14719d

See more details on using hashes here.

File details

Details for the file pyppeteer_fork-0.0.27-py2.py3-none-any.whl.

File metadata

  • Download URL: pyppeteer_fork-0.0.27-py2.py3-none-any.whl
  • Upload date:
  • Size: 80.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.0.0 pkginfo/1.5.0.1 requests/2.20.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.7.3

File hashes

Hashes for pyppeteer_fork-0.0.27-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 c54ebd9d8aa386d71362e5c25252d1d7983284225e29cf18a65c226c43073e44
MD5 a6a8cd61267f280e87585e8eeb851513
BLAKE2b-256 cb2c5b4c68425e04473213cceeb0608fa1efda923abce00f1d8c1b5297bcc63c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page