2013-10-15 54 views
0

听起来很简单,但我是新的整个屏幕报废的东西。我已经是一个远程站点http://www.remotesite.com(例如目的),有计划表,象这样的结构:屏幕抓取PHP的两列表

<table> 
    <tr> 
    <td class="team"> 
     Team 1 
    </td> 
    <td class="team"> 
     Team 2 
    </td> 
    </tr> 
</table> 

表中填充取决于游戏持续了数项的动态范围第一队和第二队等。

我已经构建了我的报废队伍,以获得表中列出的所有队伍的列表,并且它可以成功运行。下面的代码:

<?php 
// Load Simple DOM 
    include_once("simple_html_dom.php"); 

// Scrape the Schedule 
    libxml_use_internal_errors(true); 
    $dom = new DOMDocument(); 
    $html = file_get_html("http://www.remotesite.com/schedule.htm"); 

    // Load HTML 
     $dom->loadHTML($html); 
     $xpath = new DOMXPath($dom); 

    // Get all the Teams 
     $my_xpath_query = "//table//td[contains(@class, 'team')]"; 
     $result_rows = $xpath->query($my_xpath_query); 

>

而且呼应刮我有这样的代码:

<?php 
    // Display the schedule 
     foreach ($result_rows as $result_object){ 
      echo $result_object->nodeValue; 
     } 
?> 

然而,这样做是呼应了像这样的球队:

Team1Team2Team3Team4Team5Team6 etc, etc. 

它正在获得正确的顺序相互对抗的球队对,但我需要做的是es基本上以我提取它的相同方式回显表格。

在此先感谢您提供的任何帮助!

+0

是什么_“什么,我需要做的是基本呼应出表,我取了同样的方式它的意思是? – Madbreaks

+0

我想要完整地获取表格并将其从我的脚本中回显到屏幕上。 – rws907

+0

为什么要搜索'// table // td ...'呢?为什么不只是表本身的XPath查询? – Madbreaks

回答

0

根据您的回答我的问题,我建议只是在做这样的事情:

$rows = ''; 
$teams = array(); 

// Pull team names into array 
foreach ($result_rows as $result_object){ 
    $teams[] = $result_object->nodeValue; 
} 

// Extract two teams per table row 
while(count($teams)){ 
    $matchup = array_splice($teams, 0, 2); 
    $rows .= '<tr><td>'.implode('</td><td>', $matchup).'</td></tr>'; 
} 

// Write out the table 
echo "<table>$rows</table>'; 
+0

感谢您的答复和迄今为止的帮助,非常感谢。当我运行你的例子时,我得到“致命错误:超过30秒的最大执行时间” – rws907

+0

oops,错字。尝试以上(具体来说,固定参数'array_splice') – Madbreaks

+0

就是这样,谢谢! – rws907