0

十分に単純に聞こえますが、私は画面全体のスクレイピングに慣れていません。私が持っているのは、次のような構造のスケジュールテーブルを持つリモートサイトhttp://www.remotesite.com (例として) です。

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

テーブルには、その日に行われるチーム 1 対チーム 2 などの試合数に応じて、動的な範囲のエントリが入力されます。

テーブルにリストされているすべてのチームのリストを取得するためにスクレイパーを作成しましたが、正常に動作します。コードは次のとおりです。

<?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.

互いに対戦しているチームのペアを正しい順序で取得していますが、基本的には、フェッチしているのと同じ方法でテーブルをエコーアウトする必要があります。

あなたが私に与えることができる助けを前もって感謝します!

4

1 に答える 1

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>';
于 2013-10-15T22:03:26.717 に答える