Last active
December 21, 2015 10:09
-
-
Save OutThisLife/6290410 to your computer and use it in GitHub Desktop.
print out all charts from a finviz chart screen page.
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 | |
/* | |
* Prints out all finviz charts from a specific screen. | |
*/ | |
// ----------------------------------------------- | |
libxml_use_internal_errors(1); | |
DEFINE('F_BASE', 'http://finviz.com/screener.ashx?v=212&f=cap_midover,fa_eps5years_o15,sh_avgvol_o750,ta_perf_52w20o&ft=4&ta=0&o=-volume&r='); | |
// ----------------------------------------------- | |
/** | |
* Grab data from finviz | |
*/ | |
function _fc($finviz) { | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $finviz); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); | |
$contents = curl_exec($ch); | |
curl_close($ch); | |
return $contents; | |
} | |
/** | |
* Reformat the image URL | |
*/ | |
function _ri($img) { | |
$img = str_replace('chart.ashx', 'http://finviz.com/chart.ashx', $img); | |
$img = str_replace('&s=m', '&s=l', $img); | |
return $img; | |
} | |
/** | |
* Get the XPATH of the dom | |
*/ | |
function getXPath($page) { | |
$init = _fc(F_BASE . $page); | |
$dom = new DOMDocument(); | |
$dom->loadHTML($init); | |
$dom->preserveWhiteSpace = false; | |
return new DOMXPath($dom); | |
} | |
/** | |
* Store the charts from a specific page | |
*/ | |
global $charts; | |
$charts = array(); | |
function getCharts(&$xpath) { | |
global $charts; | |
foreach ($xpath->query('//*[@class="charts-gal"]') AS $img) | |
array_push($charts, _ri($img->getAttribute('src'))); | |
} | |
?> | |
<!DOCTYPE html> | |
<html class="no-js"> | |
<head> | |
<style> | |
* { | |
-webkit-box-sizing: border-box; | |
-moz-box-sizing: border-box; | |
box-sizing: border-box; | |
} | |
body img { | |
float: left; | |
width: 50%; | |
height: auto; | |
} | |
</style> | |
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/normalize/2.1.0/normalize.min.css" /> | |
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.0-rc2/css/bootstrap.min.css" /> | |
</head> | |
<body> | |
<?php | |
# Retrieve and store all charts from every page | |
$xpath = getXPath('1'); | |
getCharts($xpath); | |
# Grab the rest, skipping page 1. | |
$pages = $xpath->query('//*[@id="pageSelect"]')->item(0); | |
foreach ($pages->childNodes AS $option): | |
$p = $option->getAttribute('value'); | |
if ($p == 1) continue; | |
$xpath = getXpath($p); | |
getCharts($xpath); | |
endforeach; | |
# Finally! Print out the charts | |
foreach ($charts AS $chart) echo '<img src="', $chart, '" />'; | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment