2012-02-10 36 views
0

我一直在尝试从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> 
+0

http://www.asx.com.au/legal/terms-use.htm *您不得使用任何蜘蛛,屏幕刮板,机器人,或其他自动化类似软件或设备(“禁止设备”)以任何方式使用或访问本网站,您也不能使用任何禁用设备(或任何类似流程)复制,下载或监控内容,而无需ASX事先书面批准。* – 2012-02-10 05:47:38

+0

啊,我绝对应该检查一下。谢谢你! 为了学习的目的,如果我从来没有实现过它,那么你可以帮我处理代码。 – 2012-02-10 06:06:52

回答

1

每个人都会理所当然地告诉你,你不能用正则表达式解析HTML,应使用HTML解析器(像这样一个在simple_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]; 

我已经测试过它,它的工作原理。为了使它更强大,我建议使用其他工具,但这应该让你离开地面。祝你好运!

+0

我明白了,我现在将调查simple_dom。我将如何去实现刚刚发布的代码? 我试图用php标签包装它,并把echo $ valueYouWant;但它只是返回PHP错误。 – 2012-02-10 06:09:10

+1

你应该能够把它放在你的行之后,$ data被定义了......它打印了什么错误?也许你的php配置没有preg功能? – hackartist 2012-02-10 06:20:19

+0

哦,我明白了,我没有包括$ data line> _ < 我会在周一再次看看这个:)感谢您的帮助! – 2012-02-11 00:52:40

0

感谢这个 - 我可以使用代码在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> 

以为有人可能会发现上述方案都放在一起的情况下,这很有用。

非常感谢您回答这个问题hackartist:)