Created
March 5, 2014 08:37
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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