Php 使用foreach循环从页面源中刮取所有类数据

Php 使用foreach循环从页面源中刮取所有类数据,php,dom,foreach,Php,Dom,Foreach,在这里,我使用DOM从网页中抓取数据。我可以为头等舱搜集资料。我已经为每个人考虑了所有的代码>复习包装器< /代码>类。我认为它是迭代的,但每次都只显示类似的结果 我正在抓取评论、日期和费率值 例如: 代码: 您可以使用for循环执行此操作: <?php libxml_use_internal_errors(true); $html= file_get_contents('http://www.yelp.com/biz/franchino-san-francisco?

在这里,我使用DOM从网页中抓取数据。我可以为头等舱搜集资料。我已经为每个人考虑了所有的代码>复习包装器< /代码>类。我认为它是迭代的,但每次都只显示类似的结果

我正在抓取评论、日期和费率值

例如:

代码:


您可以使用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/>";
        }
    }
    ?>

你为什么不使用YelpAPI?这是你的第三个问题lol:)
  <?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/>";
        }
    }
    ?>