forked from rgdalessandro/ipohype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pricingscrape.php
71 lines (51 loc) · 2.19 KB
/
pricingscrape.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/php
<?php
// script to scrape for the IPO offer pricing
error_reporting( E_ERROR | E_PARSE ); // this line facilitates debugging this script
include_once( 'simple_html_dom.php' ); // scraping script
include_once( 'connection.php' );
$symbols = getSymbols();
$ipohtml = file_get_html('http://www.iposcoop.com/index.php?option=com_content&task=view&id=1544&Itemid=147');
$ipoRet = $ipohtml->find("table",14);
$ipos = array();
$counter = 0;
foreach ( $ipoRet->find("tr") as $bigrow )
{
$counter++;
if ( $counter == 1 ) continue; // this if statement with counter is used to skip the header row of the table
$t = 0;
foreach ( $bigrow->find("tr") as $row ) // loop through all IPOs in the table and scrape values
{
$ipo = array();
$symbol = $row->find("td",1)->plaintext;
$ipoprice = $row->find("td",5)->plaintext;
$closeprice = $row->find("td",6)->plaintext;
if ( !empty( $symbol ) && in_array($symbol, $symbols) ) // only write to database if the IPO is in the database
{
$markethtml = file_get_contents('https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.historicaldata%20where%20symbol%20%3D%20%22' . $symbol . '%22%20and%20startDate%20%3D%20%22' . $ipodate . '%22%20and%20endDate%20%3D%20%22' . $ipodate . '%22&format=json&diagnostics=false&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=');
$marketdata = json_decode($markethtml, true);
$ipo["symbol"] = $symbol;
$ipo["ipoprice"] = substr($ipoprice, 7);
$ipo["closeprice"] = substr($closeprice, 7);
$ipos[] = $ipo;
}
}
}
$con = new connection();
foreach ($ipos as $ipo) // insert values into database
{
$sql = "UPDATE ipo SET ipoprice = '" . mysql_escape_string($ipo['ipoprice']) . "', closeprice = '" . mysql_escape_string($ipo['closeprice']) . "' WHERE symbol = '" . $ipo['symbol'] . "'";
$con->execute($sql);
}
function getSymbols() { // get a list of IPOs from database that are missing the ipoprice
$sql = "SELECT DISTINCT symbol FROM ipo WHERE ipoprice IS NULL";
$con = new connection();
$con->execute($sql);
$symbols = array();
while ( $con->fetch() )
{
$symbols[] = $con->rows["symbol"];
}
return $symbols;
}
?>