php抓取页面循环

时间:2016-02-16 06:09:40

标签: php loops curl web-scraping

我有一个完全显示抓取页面结果的循环。现在我放置一个循环,我需要提取到最后一个页面的数量。我正在获取分页最后一个值并将其放入for循环中。让我们假设1到1474是范围。 现在我想在给定的时间内抓取网址。假设page = 1,page = 2,.... page = 1474。并希望在ul li sets中显示结果。

以下是我目前工作的代码。请注意,因为它显示了大约1474次的第一页数据。

$ch = curl_init('http://www.qatarliving.com/v3/classifieds/search/category/mobile-devices');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36');

$res = curl_exec($ch);

if ($res === false) {
    die('error: ' . curl_error($ch));
}

curl_close($ch);
$d = new DOMDocument();
@$d->loadHTML($res);
$x = new DOMXPath($d);

$review = $x->query('//p[@class="b-filters-block--el-title"]');
if($review->length > 0) {
    foreach($review as $row){
        echo "<h1>".$row->nodeValue . "</h1>";
    }
}
$pagging = $x->query('//a[@class="b-pagination--el-page b-pagination--el-item"]');

if($pagging->length > 0) {
    echo "<ul>";
    foreach($pagging as $row){
        $tag= '';
        $ddt= $row->getAttribute('href');
        $url = str_replace('http://www.qatarliving.com/v3/classifieds/search/category/mobile-devices?page=','',$ddt);

        $array[$tag] = $url;
    }
    $arrayvalue = end($array);
    echo "[ Last pagging value is : " . end($array) . " ]<br><br>";
        $myname = $x->query('//p[@class="b-card--el-description"]');
        for ($x = 1; $x <= $arrayvalue; $x++) {
        echo "<h3>Page = " . $x . "</h3>";
        foreach ($myname as $tag) {
           echo "<li> " . $tag->nodeValue . "</li>";
        }
    }
    echo "</ul>";
}

2 个答案:

答案 0 :(得分:0)

类似的东西

for($lp = 1; $lp <= $arrayvalue; $lp++){
$url="http://www.qatarliving.com/v3/classifieds/search/category/mobile-devices?page=" . $lp;
$html = file_get_contents($url);
$doc = new DOMDocument();
@$doc->loadHTML($html);
$xpath = new DOMXPath($d);
$myname = $xpath->query('//p[@class="b-card--el-description"]');
echo "<div><h1>".$url."</div>";
    foreach($myname as $items){
                echo $items->nodeValue . "<br>";
            }
        echo "</div>";
}

答案 1 :(得分:0)

这不会给出预期的结果。

$ch = curl_init('http://www.qatarliving.com/v3/classifieds/search/category/mobile-devices');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36');
$res = curl_exec($ch);
if ($res === false) {die('error: ' . curl_error($ch));}
curl_close($ch);
$d = new DOMDocument();
@$d->loadHTML($res);
$x = new DOMXPath($d);
$output = array(
    'class' => '',
    'src' => '',
);

$pagging    = $x->query('//a[@class="b-pagination--el-page b-pagination--el-item"]');
$myspan     = $x->query('//div[@class="b-items-list"]');

$data_array = array();
if($myspan->length > 0) {
        $array = array();
    foreach ($myspan as $row) {


        $data = $x->query('//span[@class="b-card b-card-mod-h item  "]');
        if($data->length > 0) {
            foreach ($data as $dt) {
                    $tag    = '';
                    $class  = $dt->getAttribute('class');
                    $value  = $dt->getAttribute('href');
                //if (trim($class) == 'b-ad-excerpt b-par-mod-clear b-line-mod-thin--mix-item'){
                    $tag='anch';
                //}
                if ($tag) {
                    $array[$tag] = $value;
                }

            }
        }


        $data = $row->getElementsByTagName('p');
        foreach ($data as $dt) {
            $tag = '';
            $class = $dt->getAttribute('class');
            $value = $dt->nodeValue;
            if ($class == 'b-card--el-deposit-val') {
                $tag = 'price';
            } else if ($class == 'b-card--el-deposit-time') {
                $tag = 'deposittime';
            } else if ($class == 'b-ad-excerpt b-par-mod-clear b-line-mod-thin--mix-item') {
                $tag = 'category';
            } else if ($class == 'b-card--el-description') {
                $tag = 'name';
            } 
            if ($tag) {
                $array[$tag] = $value;
            }
        }

        $data = $row->getElementsByTagName('a');
        foreach ($data as $dt) {
            $tag = '';
            $class = $dt->getAttribute('class');
            $value = $dt->nodeValue;
            if (trim($class) == 'b-card--el-view img-responsive') {
                $tag = 'addedby';
            } else if(trim($class) == 'b-card--el-agency-title'){
                $tag = 'by';
            }
            if ($tag) {
                $array[$tag] = $value;
            }
        }

        $data = $row->getElementsByTagName('img');
        foreach ($data as $dt) {
            $tag = '';
            $class = $dt->getAttribute('class');
            $src = $dt->getAttribute('src');
            $value = $dt->getAttribute('src');
            if (trim($class) == 'b-card--el-view img-responsive') {
                $tag = 'image';
            } else if (!empty($src)) {
                $tag = 'image';
                $value = "no.jpg";
            } else if (trim($class) == 'b-pagination--el-page') {
                $tag = 'pagging';
            }
            if ($tag) {
                $array[$tag] = $value;
            }
        }

        $data_array[] = $array;
    }

    echo "<pre>";
    print_r($data_array);
    echo "</pre>";

}