I have been trying to data scrape the current value of a stock from the ASX.com.au website. Namely, I am trying to grab the current value of the ASX. This can be found here.
http://www.asx.com.au/asx/markets/equityPrices.do?by=asxCodes&asxCodes=asx
It is the second td from the left, at the time of writing this it is sitting at 30.410.
I can played around with some code and have not been able to get it to work.
Below is the sample code I have been toying with, if anyone is able to help me to get this to work I would be grateful!
<?php
$data = file_get_contents('http://www.asx.com.au/asx/markets/equityPrices.do?by=asxCodes&asxCodes=asx');
$asx = explode('<th class="row" scope="row">ASX: </th>', $data);
$asx = substr($asx[1], 4, strpos($asx[1], '</td>') - 4);
?><div class="asxvalue"><?php echo $asx . "<br />\n";?></div>
EDIT
Update of code
<?php
$data = file_get_contents('http://www.asx.com.au/asx/research/companyInfo.do?by=asxCode&asxCode=DTL');
preg_match('/<td class="last">([^<]*?)</td>/i',$data,$matches);
$valueYouWant = $matches[1];
?><div class="data"><?php echo $valueYouWant ?></div>
Everyone will rightfully tell you that you can’t parse html with regex and should use an html parser (like this one at simple_dom) but for your specific problem you can do this:
To find the value of the date and last on the other page you can use the following:
I actually would suggest using Simple_Dom for things like this in the future but until you are comfortable with it this will work for now:
I have tested this and it works. To make it more robust I recommend using other tools but this should get you off the ground. Good Luck!