Git Product home page Git Product logo

pharo-chrome's Introduction

Pharo-Chrome

Pharo-Chrome provides a simple API for Google Chrome's DevTools protocol (https://chromedevtools.github.io/devtools-protocol/).

The are three main public classes in the package:

  • GoogleChrome is the interface to the Chrome Browser
  • ChromeTabPage is the interface to a single tab / page in the browser
  • ChromeNode is a single DOM node within a page, with a similar interface to Soup

A ZnEasy like interface is provided for retrieving pages:

GoogleChrome get: 'http://pharo.org'

An example of more detailed operations are available in GoogleChrome class>>exampleNavigation.

Features

  • A ZnEasy like feature for retrieving pages: #get:
  • Extraction of tabular data (html tables): #extractTables, #tableData
  • Screenshot capture: #captureScreenshot
  • Headless mode

Installation

Unix platforms (Linux & MacOS) require OSProcess or OSSubprocess to be installed prior to loading Pharo-Chrome. To load OSSubprocess in 32 bit images:

Metacello new
	configuration: 'OSSubprocess';
	repository: 'github://marianopeck/OSSubprocess:master/repository';
	version: #stable;
	load.

On Windows the OSWindows package is required:

Metacello new
	configuration: 'OSWindows';
	repository: 'http://smalltalkhub.com/mc/Pharo/MetaRepoForPharo60/main/';
	load.

Once the platform dependent packages have been loaded, load the main Pharo-Chrome package:

Metacello new
	baseline: 'Chrome';
	repository: 'github://astares/Pharo-Chrome:master/repository';
	load.

To keep the original Monticello idea of having #stable and #development versions, Pharo-Chrome is mostly using the GitFlow workflow:

  • The master branch is #stable
  • The development branch is #development

A Short Demo

Stock price retrieval

As a demonstration of Pharo-Chrome, we'll retrieve some stock prices from the Australian ASX S&P 200 stock index:

rootNode := GoogleChrome get: 'https://finance.yahoo.com/quote/%5EAXJO/history?p=%5EAXJO'

The messages I use most for navigating the DOM are:

#children
Answer an array of the child nodes
#parent
Answer the parent node of the receiver
#findAllTags:
Find all tags matching the supplied criteria (see below).
#findAllStrings:
Find all strings matching the supplied criteria.
#allSelect:
Answer all nodes matching the supplied criteria.

Both #findAllTags: and #findAllStrings: can take a string, a collection or a boolean as the parameter. E.g. to find the h1 and h2 headings:

headings := rootNode findAllTags: #('h1' 'h2').

More interesting is extracting all the data from a table, e.g. historical prices:

tables := rootNode extractTables.

At the time of writing, two tables were returned. The larger table is the one we're interested in. Most of the rows will be actual data, smaller rows typically contain comments, so we'll make the data uniform by simply rejecting smaller rows. It can be nicely inspected using:

historicalData := (tables sorted: #size ascending) last.
dataFrame := DataFrame fromRows: (historicalData select: [ :each | each size = 7 ]).
dataFrame asStringTable.

"
     |  1             2         3         4         5         6            7       
-----+-----------------------------------------------------------------------------
1    |  Date          Open      High      Low       Close*    Adj Close**  Volume  
2    |  Nov 14, 2017  6,021.80  6,021.80  5,957.10  5,966.00  5,966.00     -       
3    |  Nov 13, 2017  6,029.40  6,029.40  6,010.70  6,021.80  6,021.80     -       
4    |  Nov 10, 2017  6,049.40  6,049.40  6,020.70  6,029.40  6,029.40     -       
etc.
"

Note that the example above assumes that DataFrame has been installed in the image:

Metacello new
  baseline: 'DataFrame';
  repository: 'github://PolyMathOrg/DataFrame';
  load.

pharo.org screenshot

To retrieve a png image of the pharo.org front page in headless mode:

| browser page image |

browser := GoogleChrome new.
browser headless: true.
browser open.
page := browser firstTab.
page get: 'http://pharo.org'.
image := page captureScreenshot.
browser closeAndExit.
image

pharo-chrome's People

Contributors

akgrant avatar akgrant43 avatar astares avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.