All Projects → benbjohnson → Phantomjs

benbjohnson / Phantomjs

Licence: mit
Go client for PhantomJS.

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to Phantomjs

MTS
Automation Tools for PHP
Stars: ✭ 111 (-60.07%)
Mutual labels:  phantomjs
crawlkit
A crawler based on Phantom. Allows discovery of dynamic content and supports custom scrapers.
Stars: ✭ 23 (-91.73%)
Mutual labels:  phantomjs
chromate
Automate Headless Chrome.
Stars: ✭ 36 (-87.05%)
Mutual labels:  phantomjs
jazeee-meteor-spiderable
Fork of Meteor Spiderable with longer timeout, caching, better server handling
Stars: ✭ 33 (-88.13%)
Mutual labels:  phantomjs
phantom-lord
Handy API for Headless Chromium
Stars: ✭ 24 (-91.37%)
Mutual labels:  phantomjs
pyCreeper
一个用来快速提取网页内容的信息采集(爬虫)框架, 实现了对网页的动态加载与控制。
Stars: ✭ 25 (-91.01%)
Mutual labels:  phantomjs
screenshot.py
Taking a screenshot of a webpage.
Stars: ✭ 49 (-82.37%)
Mutual labels:  phantomjs
node-qunit-phantomjs
Run QUnit unit tests in a headless PhantomJS instance without using Grunt
Stars: ✭ 36 (-87.05%)
Mutual labels:  phantomjs
siteshooter
📷 Automate full website screenshots and PDF generation with multiple viewport support.
Stars: ✭ 63 (-77.34%)
Mutual labels:  phantomjs
img-cli
An interactive Command-Line Interface Build in NodeJS for downloading a single or multiple images to disk from URL
Stars: ✭ 15 (-94.6%)
Mutual labels:  phantomjs
ComicSpider
动漫之家漫画站电脑版原图爬虫
Stars: ✭ 67 (-75.9%)
Mutual labels:  phantomjs
pyderman
Install Selenium-compatible Chrome/Firefox/Opera/PhantomJS/Edge webdrivers automatically.
Stars: ✭ 24 (-91.37%)
Mutual labels:  phantomjs
intransient capybara
A set of improvements to Minitest/Capybara/Poltergeist/PhantomJS test stack that reduces the occurrence of transient failures.
Stars: ✭ 25 (-91.01%)
Mutual labels:  phantomjs
phantomic
Pipe stdin to Phantom.JS
Stars: ✭ 20 (-92.81%)
Mutual labels:  phantomjs
java-phantomjs-wrapper
A Java wrapper around the PhantomJS binaries including a packaged HTML to PDF render script
Stars: ✭ 54 (-80.58%)
Mutual labels:  phantomjs
TRA-Ticket-Booker
(已不適用新版臺鐵訂票系統,且不再更新)台灣鐵路訂票應用程式(臺鐵 / 台鐵 / 訂單程票 / 訂來回票),基於 Selenium + PyQt4。
Stars: ✭ 26 (-90.65%)
Mutual labels:  phantomjs
kick-off-web-scraping-python-selenium-beautifulsoup
A tutorial-based introduction to web scraping with Python.
Stars: ✭ 18 (-93.53%)
Mutual labels:  phantomjs
Responsive mockups
Takes screenshots of a webpage in different resolutions and automatically applies it to mockup templates.
Stars: ✭ 274 (-1.44%)
Mutual labels:  phantomjs
wdm4j
Automatic Selenium WebDriver binaries management for java
Stars: ✭ 16 (-94.24%)
Mutual labels:  phantomjs
TqExtension
Test your Drupal 7 (D8 in progress) sites easier with TqExtension for Behat.
Stars: ✭ 13 (-95.32%)
Mutual labels:  phantomjs

deprecation warning

active phantomjs development has ended, in favor of using Chrome's new headless functionality (reference). Instead of using this library, consider using a go package that uses this new api such as chromedp.

phantomjs godoc Status

This is a Go wrapper for the phantomjs command line program. It provides the full webpage API and has a strongly typed API. The wrapper provides an idiomatic Go interface while allowing you to communicate with the underlying WebKit and JavaScript engine in a seamless way.

Installing

First, install phantomjs on your machine. This can be done using your package manager (such as apt-get or brew). Then install this package using the Go toolchain:

$ go get -u github.com/benbjohnson/phantomjs

Usage

Starting the process

This wrapper works by communicating with a separate phantomjs process over HTTP. The process can take several seconds to start up and shut down so you should do that once and then share the process. There is a package-level variable called phantomjs.DefaultProcess that exists for this purpose.

package main

import (
	"github.com/benbjohnson/phantomjs"
)

func main() {
	// Start the process once.
	if err := phantomjs.DefaultProcess.Open(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	defer phantomjs.DefaultProcess.Close()

	// Do other stuff in your program.
	doStuff()
}

You can have multiple processes, however, you will need to change the port used for each one so they do not conflict. This library uses port 20202 by default.

Working with WebPage

The WebPage will be the primary object you work with in phantomjs. Typically you will create a web page from a Process and then either open a URL or you can set the content directly:

// Create a web page.
// IMPORTANT: Always make sure you close your pages!
page, err := p.CreateWebPage()
if err != nil {
	return err
}
defer page.Close()

// Open a URL.
if err := page.Open("https://google.com"); err != nil {
	return err
}

The HTTP API uses a reference map to track references between the Go library and the phantomjs process. Because of this, it is important to always Close() your web pages or else you can experience memory leaks.

Executing JavaScript

You can synchronously execute JavaScript within the context of a web page by by using the Evaluate() function. This example below opens Hacker News, retrieves the text and URL from the first link, and prints it to the terminal.

// Open a URL.
if err := page.Open("https://news.ycombinator.com"); err != nil {
	return err
}

// Read first link.
info, err := page.Evaluate(`function() {
	var link = document.body.querySelector('.itemlist .title a');
	return { title: link.innerText, url: link.href };
}`)
if err != nil {
	return err
}

// Print title and URL.
link := info.(map[string]interface{})
fmt.Println("Hacker News Top Link:")
fmt.Println(link["title"])
fmt.Println(link["url"])
fmt.Println()

You can pass back any object from Evaluate() that can be marshaled over JSON.

Rendering web pages

Another common task with PhantomJS is to render a web page to an image. Once you have opened your web page, simply set the viewport size and call the Render() method:

// Open a URL.
if err := page.Open("https://news.ycombinator.com"); err != nil {
	return err
}

// Setup the viewport and render the results view.
if err := page.SetViewportSize(1024, 800); err != nil {
	return err
}
if err := page.Render("hackernews.png", "png", 100); err != nil {
	return err
}

You can also use the RenderBase64() to return a base64 encoded image to your program instead of writing the file to disk.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].