我一直试图从ASX.com.au网站上收集一只股票的当前价值。也就是说,我试图获取ASX的当前值。这个可以在这里找到。
http://www.asx.com.au/asx/markets/equityPrices.do?by=asxCodes&asxCodes=asx
这是从左开始的第二个td,在写这篇文章的时候,它是在30.410。
我可以玩一些代码,但一直无法使它工作。
下面是我一直在玩的示例代码,如果有人能帮我完成这个工作,我将不胜感激!
<?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>编辑
代码的更新
<?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>发布于 2012-02-10 05:45:29
每个人都会正确地告诉您,您不能用regex解析html,应该使用html解析器(就像dom中的这个解析器),但是对于特定的问题,您可以这样做:
preg_match('/<td class="last">([^<]*?)</td>/i',$data,$matches);
$valueYouWant = $matches[1];要在另一个页面上找到日期和最后一个的值,您可以使用以下方法:实际上,我建议在将来使用Simple_Dom来处理类似的事情,但是在您对它感到满意之前,这个方法现在是可行的:
$data = file_get_contents('http://www.asx.com.au/asx/research/companyInfo.do?by=asxCode&asxCode=DTL');
preg_match('/id="closing-prices".*?<strong>(.*?)<\/strong>.*?<td class="last">(.*?)<\/td>/s',$data,$matches);
$date = $matches[1];
$lastValue = $matches[2];我已经测试过了,而且效果很好。为了使它更加健壮,我建议使用其他工具,但这应该会使您脱离困境。祝好运!
发布于 2014-07-16 04:23:29
谢谢-我能够在Wordpress PHP代码小部件中使用代码,这对ASX股票价格来说是个不错的选择:
<?php
$data = file_get_contents('http://www.asx.com.au/asx/research/companyInfo.do?by=asxCode&asxCode=asx');
preg_match('/id="closing-prices".*?<strong>(.*?)<\/strong>.*?<td class="last">(.*?)<\/td>/s',$data,$matches);
$date = $matches[1];
$lastValue = $matches[2];
?><div class="data">$<?php echo $lastValue ?></div>我想可能有人会找到以上所有的解决方案,以防有用。
非常感谢你回答这个问题,黑客:)
https://stackoverflow.com/questions/9223239
复制相似问题