Skip to content

Instantly share code, notes, and snippets.

@namanyayg
Created March 5, 2014 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save namanyayg/9363421 to your computer and use it in GitHub Desktop.
Save namanyayg/9363421 to your computer and use it in GitHub Desktop.
A simple script that uses phpQuery-onefile and custom PHP code to create a json file from scraping coinmarketcap.
<?php
require('phpQuery-onefile.php');
$page = phpQuery::newDocumentFileHTML('http://coinmarketcap.com/');
$cryptoprices = 'var cryptoprices = {';
$cryptoprices .= 'markets:[';
foreach( pq('tbody tr') as $row ) {
$id = pq($row)->attr('id');
$img = pq($row)->find('img')->attr('src');
$name = pq($row)->find('.currency-name a')->text();
$url = pq($row)->find('.currency-name a')->attr('href');
$price = pq($row)->find('.price')->attr('data-usd');
$cryptoprices .= '{name:"' . $name . '",id:"' . $id . '",img:"' . $img . '",url:"' . $url . '",price:"' . $price . '"},';
};
$cryptoprices = substr($cryptoprices, 0, -1);
$cryptoprices .= '],';
$cryptoprices .= 'timestamp:"' . pq('.row > .col-xs-12 > p.small')->text() . '"';
$cryptoprices .= '};';
if ($cryptoprices != '' || $cryptoprices != 0) {
file_put_contents('static/js/cryptoprices.js', $cryptoprices);
echo 'Everything went well! Updated on ' . date('l jS \of F Y h:i:s A');
} else {
echo 'Oops, something went wrong. Here\'s what Cryptoprices looks like: ' . $cryptoprices;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment