1

name=""Web サイトのすべての属性を抽出したいのですが、

html の例

<div class="link_row">
    <a href="" class="listing_container" name="7777">link</a>
</div>

次のコードがあります。

<?php
$html = new DOMDocument();
@$html->loadHtmlFile('http://www.onedomain.com/plus?ca=11_c&o=1');
$xpath = new DOMXPath( $html );
$nodelist = $xpath->query( "//div[@class='link_row']/a[@class='listing_container']/@name" );
foreach ($nodelist as $n){
    echo $n->nodeValue."\n<br>";
}
?>

結果は次のとおりです。

7777

このコードは正常に機能していますが、1 つのポケットベル番号に限定する必要はありません。

http://www.onedomain.com/plus?ca=11_c&o=1 ページャー属性は"o=1"

で終了したら 、定義済みの変数が等しいo=1に続いてくださいo=2$last=556http://www.onedomain.com/plus?ca=11_c&o=556

私たちを手伝ってくれますか?それを行う最良の方法は何ですか?

ありがとう

4

1 に答える 1

1

for (または while) ループを使用します。提供されたコードには表示されない$lastため、最大値に 1 を加えた値を静的に設定しました。

$html = new DOMDocument();
for($i =1; $i < 557; $i++) {
    @$html->loadHtmlFile('http://www.onedomain.com/plus?ca=11_c&o=' . $i);
    $xpath = new DOMXPath( $html );
    $nodelist = $xpath->query( "//div[@class='link_row']/a[@class='listing_container']/@name" );
    foreach ($nodelist as $n){
        echo $n->nodeValue."\n<br>";
    }
}

より簡単な例:

for($i =1; $i < 557; $i++) {
    echo $i;
}

http://php.net/manual/en/control-structures.for.php

于 2015-07-07T05:02:48.157 に答える