0

私がつかんでいるHTMLは次のようになります

<div id="table"><table>
<tr><td>Clubname</td><td>15</td><td>30</td></tr>
<tr><td>Clubname 2</td><td>15</td><td>30</td></tr>
<tr><td>Clubname 3</td><td>15</td><td>30</td></tr>
</table></div>

私が欲しいのは、Clubname 2が入っているtrを見つけて、td[1]とtd[2]からデータを取得し、これを出力することです。

simple_html_dom.phpでこれを実行したい

私がすでに持っているのは

require('simple_html_dom.php');
$html = file_get_html('webpage.html');
foreach($html->find('div#table') as $e)
echo $e->innertext . '<br>';

特定のクラブ名を見つけて、同じtrのtdから特定のコンテンツを取得するにはどうすればよいですか?

================================================== ===============================

さて、ありがとう、私が今やったことは、後で変数を使いたいので、変数だけであなたが私に言ったようです。

<?php
    $clubname = 'Ajax';
    require('phpQuery/phpQuery.php');         
    $result = array();
    $limit = 2; //you need only the next two sibling

    $dom = phpQuery::newDocumentFile('http://soccer.gijsve.nl/test2.php');        
    $match = $dom->find('td:contains("'.$clubname.'")');        
    while( 0 < count( $match = $match->next() ) && ( 0 < $limit-- )  ){
            $result[] = $match->text();
    }
    var_dump($result);

?>

私が今欲しいのは、たとえば最初のtd(試合前のtd)と4番目と5番目を選択することです。得点、得点、ランクを知る必要があるからです。私がつかんでいるテーブルについては、http://soccer.gijsve.nl/test2.phpを参照してください。

4

2 に答える 2

2

他の簡単なDOMツールをお勧めします: http : //code.google.com/p/phpquery/私が経験したように、それは少し速く、セレクターはより良く機能します。このプロジェクトはジェット機で完成していません。しかし、domreadingセクションはうまく機能します。jQueryのように機能するセレクター;)

<?php
    require('phpquery/phpQuery.php');         
    $result = array();
    $limit = 2; //you need only the next two sibling

    $dom = phpQuery::newDocumentFile('webpage.html');        
    $match = $dom->find('td:contains("Clubname 2")');        
    while( 0 < count( $match = $match->next() ) && ( 0 < $limit-- )  ){
            $result[] = $match->text();
    }
    var_dump($result);

    // other example:
    $match = $dom->find('td:contains("Clubname 2")');        
    $loses = $match->siblings('.loses')->text(); //matches the siblings of the $match element, and has loses class
    $wins = $match->siblings('.wins')->text(); //matches the siblings of the $match element, and has wins class

?>

これは、サンプルhtml、およびコメントした完全なhtmlで機能します。

simple_html_domを使用すると、テキスト「タグ」としてテキストを検索できます。

$textlist = $html->find('text');

これにより、すべてのテキストブロックが返され、foreach $ textlistを実行する必要があります。要素のhtmlが見つかったものと等しい場合は、それを変数に保存し、この親に戻ります(完全なhtmlでは、2番目親)次の兄弟、そしてまた次の兄弟へのステップよりも、phpQueryよりも複雑だと思います。

于 2013-03-12T12:13:59.120 に答える
1

Phpqueryには、生活を楽にする兄弟セレクターもあります。

$dom->find('td:contains("Clubname 2") + td')->text(); # 15
$dom->find('td:contains("Clubname 2") + td + td')->text(); # 30
$dom->find('td:contains("Clubname 2") ~ td')->text(); # 15 30
于 2013-03-12T20:32:27.940 に答える