You can install wring using npm:
$ npm install --global wring
Wring utilizes PhantomJS for some of its commands. To
use these, install it using your system package manager by running something
brew install phantomjs on OS X, or
apt-get install phantomjs on Ubuntu. You can
make sure it's on your
PATH by running
Alternatively, you can install a version which automatically downloads PhantomJS binaries for your system:
$ npm install --global wring-with-phantomjs
Here is a simple example which prints contents of the matching element (uses Cheerio under the hood):
$ wring text '' '#currency_converter_result'1 EUR = 1.0940 USD# You can use the first letter of command as a shortcut$ wring t http://randomfunfacts.com/ 'i'No president of the United States was an only child.
You can also use jQuery specific selectors such as
$ wring t '' 'tr:contains("The Hunger Games") th:first-child'"Eyes Open""Safe & Sound"
wring html prints
outerHTMLof matching elements. Let's try it, this time
using an XPath expression:
$ wring html "" "//td[@class='title']/a[starts-with(@href,'http')]"<a href="">PostgreSQL Indexes: First principles</a><a href="">Doing Mathematics Differently</a><a href="">The rise of the API-based SaaS</a><a href="">Rich Hickey Fanclub</a>...
First argument of a command specifies its input, which can be a URL, path to a
file, HTML string, or
- to read the page source from
Using with PhantomJS
Prefixing a command with
p will run it using jQuery inside a
real web browser context. You can use this if you are having compatibility
problems with the commands above, but the real utility comes from being able to
scrape dynamically generated content:
$ wring p t '<title>Foo</title> <script>document.title = "Bar";</script>' 'title'Bar# compare it to the non-phantomjs invocation below$ wring t '<title>Foo</title> <script>document.title = "Bar";</script>' 'title'Foo
wring('str') will write to terminal. You can pass any number of .js file
paths, URLs, and JS expressions as script arguments and they will get executed
in given order:
$ wring eval '' 'wring(document.title)'IPFS is a new peer-to-peer hypermedia protocol.# you can load and use third party libraries:$ wring e '' '' 'wring(_.kebabCase(document.title))'ipfs-is-a-new-peer-to-peer-hypermedia-protocol
You can also use a trick to make self contained scripts.
Here is a contrived example which loads Hacker News homepage, loads lodash, sorts posts by their score, and prints the top 5:
#!/bin/sh":" //; exec wring eval "" "" "$0"var posts = _var scores = _
# after saving the source above to `wring_hn.js` you can run it like this$ chmod +x wring_hn.js$ ./wring_hn.jsRaspberry Pi 3 Model B confirmed, with onboard BT LE and WiFihttps://apps.fcc.gov/oetcf/eas/reports/...After fifteen years of downtime, the MetaFilter gopher server is backhttp://metatalk.metafilter.com/24019/......
Last command to cover is
wring shot, which renders a screenshot of first
matching element and saves it to a file:
$ wring shot '' '#price-panel' goog.pngwring: Saved to goog.png
goog.png will contain something like this:
# Install Node.js dependencies:$ npm install# Install PureScript dependencies:$ bower install# Build `wring.js` and `phantom-main.js`:$ npm run build# Run tests:$ npm test# Compile & run using Pulp ():$ pulp run text '<b>foo</b>' 'b'