使用foreach循环从页面源中删除所有类数据

时间:2014-04-28 12:26:10

标签: php dom foreach

这里我使用DOM从网页抓取数据。我可以废弃头等舱的数据。我已经为每个人考虑了所有review-wrapper课程。我认为它是迭代的,但每次只显示类似的结果。

我正在取消审核,日期和费率值。

示例:http://codepad.viper-7.com/lHS9jk

代码:

<?php

libxml_use_internal_errors(true);
$html= file_get_contents('http://www.yelp.com/biz/franchino-san-francisco?start=80');

$html = escapeshellarg($html) ;
$html = nl2br($html);

$classname = 'review-wrapper';
$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$results = $xpath->query("//*[@class='" . $classname . "']");

foreach($results as $node)
{
    $classname = 'rating-qualifier';
    $dom = new DOMDocument;
    $dom->loadHTML($html);
    $xpath = new DOMXPath($dom);
    $results = $xpath->query("//*[@class='" . $classname . "']");

    if ($results->length > 0) {
        echo $review = $results->item(0)->nodeValue;
        echo "<br/>";
    }


    $classname = 'review_comment ieSucks';
    $dom = new DOMDocument;
    $dom->loadHTML($html);
    $xpath = new DOMXPath($dom);
    $results = $xpath->query("//*[@class='" . $classname . "']");

    if ($results->length > 0) {
        echo $review = $results->item(0)->nodeValue;
        echo "<br/>";
    }

    $meta = $dom->documentElement->getElementsByTagName("meta");
    echo $meta->item(0)->getAttribute('content');
    echo "<br/>";
}
?>

1 个答案:

答案 0 :(得分:0)

你可以使用for循环来完成这个:

  <?php

    libxml_use_internal_errors(true);
    $html= file_get_contents('http://www.yelp.com/biz/franchino-san-francisco?start=80');

    $html = escapeshellarg($html) ;
    $html = nl2br($html);

    for ($x=0; $x<=$results->length; $x++) {
        $classname = 'rating-qualifier';
        $dom = new DOMDocument;
        $dom->loadHTML($html);
        $xpath = new DOMXPath($dom);
        $results = $xpath->query("//*[@class='" . $classname . "']");

        if ($results->length > 0) {
            echo $review = $results->item($x)->nodeValue;
            echo "<br/>";
        }


        $classname = 'review_comment ieSucks';
        $dom = new DOMDocument;
        $dom->loadHTML($html);
        $xpath = new DOMXPath($dom);
        $results = $xpath->query("//*[@class='" . $classname . "']");

        if ($results->length > 0) {
            echo $review = $results->item($x)->nodeValue;
            echo "<br/>";
        }

        $dom = new DOMDocument;
        $dom->loadHTML($html);
        $xpath = new DOMXPath($dom);
        $metas = $xpath->query("//meta[@itemprop='ratingValue']");
        if ($metas->length > 0) {
            echo $review = $metas->item($x)->getAttribute('content');
            echo "<br/>";
        }
    }
    ?>

演示:http://codepad.viper-7.com/C6KRW2