2016-11-12 30 views
0

我用下面的代码来解析从澳大利亚证券交易所表元素:解析从表链接用PHP

<?php 

$dom = new DOMDocument(); 


//load the html 
$html = $dom->loadHTMLFile("http://www.asx.com.au/asx/statistics/prevBusDayAnns.do"); 

    //discard white space 
$dom->preserveWhiteSpace = false; 

    //the table by its tag name 
$tables = $dom->getElementsByTagName('table'); 

    //get all rows from the table 
$rows = $tables->item(0)->getElementsByTagName('tr'); 
    // get each column by tag name 
$cols = $rows->item(0)->getElementsByTagName('th'); 
$row_headers = NULL; 
foreach ($cols as $node) { 
    //print $node->nodeValue."\n"; 
    $row_headers[] = $node->nodeValue; 
} 

$table = array(); 
    //get all rows from the table 
$rows = $tables->item(0)->getElementsByTagName('tr'); 

foreach ($rows as $row) 
{ 
    // get each column by tag name 

    $cols = $row->getElementsByTagName('td'); 

    $companysymbol = $cols->item(0)->nodeValue; 
    $pubtime = $cols->item(1)->nodeValue; 
    $newstitle = $cols->item(3)->nodeValue; 

    $row = array(); 

    echo $companysymbol . '<br>'; 
    echo $pubtime . '<br>'; 
    echo $newstitle . '<br><br>'; 

} 

?> 

的代码工作正常,但除了呼应$ companysymbol,$ pubtime和$ newstitle我想回显表格内的链接(PDF链接)。有人能告诉我如何?

在此先感谢您的帮助!

回答

0

你需要得到href属性,然后重新创建链接。

(...) 
$pdflink = $cols->item(5)->nodeType === XML_ELEMENT_NODE ? $cols->item(5)->getElementsByTagName('a')->item(0)->getAttribute('href') : ''; 
(...) 
echo "<a href='http://www.asx.com.au$pdflink'>pdf</a>".'<br>'; 

这将创建一个可点击的链接到PDF。

+0

谢谢回答,但不幸的是您的解决方案将回声单词“PDF”,而不是链接本身(HREF内标签)。 – Tim

+0

完美的作品!感谢1000次! – Tim