No project description provided
Project description
Phasma: PhantomJS Driver for Python
Phasma is a Python library that provides a clean, high-level interface to PhantomJS, enabling headless browser automation, web page rendering, and JavaScript execution. It simplifies the process of downloading, managing, and interacting with PhantomJS, making it ideal for web scraping, screenshot capture, and automated testing.
Features
- Bundled Driver: PhantomJS driver is included in the package (Windows, macOS, Linux). No separate download needed.
- Page Rendering: Render HTML files, strings, and remote URLs with JavaScript support.
- JavaScript Execution: Run arbitrary JavaScript code in a PhantomJS context.
- CLI Interface: Command-line tools for quick operations.
- Cross‑Platform: Works on Windows, Linux, and macOS.
- Lightweight: Minimal dependencies, focused on simplicity and reliability.
Installation
Install from PyPI:
pip install phasma
The package includes the PhantomJS driver for Windows, macOS, and Linux (both 32‑bit and 64‑bit). No separate download is required.
Or install from source:
git clone https://github.com/MohammadRaziei/phantomjs-driver.git
cd phantomjs-driver
pip install -e .
Quick Start
Using the Python API
from phasma import render_page, render_url, execjs
# The PhantomJS driver is bundled with the package, ready to use.
# Render an HTML file
html = render_page("path/to/page.html")
print(html)
# Render a URL
content = render_url("https://example.com", wait_time=2000)
print(content[:500]) # first 500 characters
# Execute JavaScript
output = execjs("console.log('Hello from PhantomJS');")
print(output)
Using the Command Line
# Show driver version
python -m phasma driver --version
# Show driver executable path
python -m phasma driver --path
# Download the driver
python -m phasma driver download
# Render an HTML file
python -m phasma render-page tests/data/test_page.html
# Render a URL
python -m phasma render-url https://example.com --wait 2000
# Execute JavaScript
python -m phasma execjs "console.log('Hello');"
API Reference
download_driver(os_name=None, arch=None)
Downloads the PhantomJS driver for the given OS and architecture. If no arguments are provided, it auto‑detects the current platform.
render_page(page, output=None, viewport_size="1024x768", wait_time=100, **kwargs)
Renders an HTML page (file path or HTML string) and returns the rendered HTML. Optionally saves the output to a file.
render_url(url, output=None, viewport_size="1024x768", wait_time=0, **kwargs)
Renders a remote URL and returns the rendered HTML. Optionally saves the output to a file.
execjs(script, args=None, **kwargs)
Executes JavaScript code in a PhantomJS context and returns the stdout.
Advanced Usage
Rendering Dynamic JavaScript Content
Phasma can render pages that modify their DOM with JavaScript:
from phasma import render_page
html_with_js = """
<html>
<body>
<div id="container">Initial</div>
<script>
document.getElementById('container').innerHTML = '<h2>Generated by JS</h2>';
</script>
</body>
</html>
"""
rendered = render_page(html_with_js, wait_time=500)
assert "<h2>Generated by JS</h2>" in rendered
Custom Viewport and Wait Time
# Render with a custom viewport and longer wait
rendered = render_page(
"page.html",
viewport_size="1920x1080",
wait_time=2000,
output="screenshot.html"
)
Testing
Run the test suite with pytest:
pytest tests/
All core functionality is covered by unit tests, including page rendering, URL fetching, and JavaScript execution.
Contributing
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
- Fork the repository.
- Create a feature branch.
- Make your changes and add tests.
- Ensure all tests pass.
- Submit a pull request.
License
Phasma is distributed under the MIT License. See the LICENSE file for details.
Acknowledgments
- PhantomJS team for the amazing headless browser.
- The Python community for excellent tooling and support.
Phasma – Making PhantomJS easy for Python developers.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file phasma-0.2.0-py3-none-win_amd64.whl.
File metadata
- Download URL: phasma-0.2.0-py3-none-win_amd64.whl
- Upload date:
- Size: 18.2 MB
- Tags: Python 3, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
851f16d221fe3f7fedbf73eb11f39a0e0bbd0e7ea77eaeec9f63b894b6388b97
|
|
| MD5 |
8ed6d8de3620d6d730096423a6aa939f
|
|
| BLAKE2b-256 |
af6baf1a4008201842346cfecf42d95c2ad9fb5417d7ee0be83674317aee65d3
|
File details
Details for the file phasma-0.2.0-py3-none-manylinux_2_17_x86_64.whl.
File metadata
- Download URL: phasma-0.2.0-py3-none-manylinux_2_17_x86_64.whl
- Upload date:
- Size: 26.2 MB
- Tags: Python 3, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c0499ef147289262b1d7cc9242098983d9c8754fa9bbbed6a404d7b44d2d2349
|
|
| MD5 |
8376429512320132e516fc71e1088837
|
|
| BLAKE2b-256 |
d632458e27310d330d1ba206003a139d9a196cef69735a5b790de0f8ba33fdd2
|
File details
Details for the file phasma-0.2.0-py3-none-manylinux_2_17_i686.whl.
File metadata
- Download URL: phasma-0.2.0-py3-none-manylinux_2_17_i686.whl
- Upload date:
- Size: 27.3 MB
- Tags: Python 3, manylinux: glibc 2.17+ i686
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
91e60ff09a3d4fc5d95dff103545a4bd4e66b9f8ea0d08efe72b9f413f7974e8
|
|
| MD5 |
b7af12d3388f746b89d0439fe18dc12d
|
|
| BLAKE2b-256 |
4625336875849959af469a995db0dbfacc00db178cd7f99c23d62185ab019bce
|
File details
Details for the file phasma-0.2.0-py3-none-macosx_10_9_x86_64.whl.
File metadata
- Download URL: phasma-0.2.0-py3-none-macosx_10_9_x86_64.whl
- Upload date:
- Size: 17.2 MB
- Tags: Python 3, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9d89d5a85f817e2237276d9a3cf8dcae60254352f044ee62204c3b738bb0ddeb
|
|
| MD5 |
01a1eca1caae12e62df8177f513692ce
|
|
| BLAKE2b-256 |
50814edb9ccf68b96e9b014e80367eaf650056823543cf28a9c6200081759abf
|