如何找到网站的页码并将其放入变量中

how to find page number of a website and put it in a variable

我在某个时候卡住了。我需要你的一点帮助。我需要找到网站的导航页面并将其放入 for 循环。我已经完成了硬编码,但我需要它动态。 以下是示例:

<?php 
for ($x = 1; $x <= 5; $x++) {
    $olxcom = file_get_contents('http://olx.com.pk/cars/?page=' . $x . '');
    $file1 = 'olx.txt';
    file_put_contents($file1 , $olxcom, FILE_APPEND);
} 
for ($y = 1; $y <= 5; $y++) {
    $pakwheels = file_get_contents('http://www.pakwheels.com/used-cars/search/-/?page=' . $y . '');
    $file2 = 'pakwheels.txt';
    file_put_contents($file2 , $pakwheels, FILE_APPEND);
} 
for ($z = 1; $z <= 5; $z++) {
    $carmudi = file_get_contents('http://www.carmudi.pk/cars/?page=' . $z . '');
    $file3 = 'carmudi.txt';
    file_put_contents($file3 , $carmudi, FILE_APPEND);
}
?>

这些数字 5 是我硬编码的页码。我试图找到那些带有 for 循环或其他东西的。 谢谢

尝试:而不是传递给你的数组;

 $dom = new DOMDocument;
 $dom->loadHTML($html);
 foreach ($dom->getElementsByTagName('a') as $node)
 {
   echo $node->nodeValue.': '.$node->getAttribute("href")."\n";
 }

使用多维数组(); http://php.net/manual/en/language.types.array.php

示例:

 <?php

 $my_array = array('x','y','z');
 for ($row = 0; $row < 3; $row++) {
     echo "<p> your stuff item ". $row."<p>";
     for ($col = 0; $col <5; $col++) {
          echo "the inner stuff";
    }
 }

 ?>

这应该可以解决您的问题...

您需要下载并解析页面的 html 才能找到页码。尝试 Simple HTML DOM Parser .

//random example of selecting from html content
$ret = $html->find('div.foo');
//OR
$ret = $html->find('div[class=foo]');

基本上你可以访问元素,就像你如何使用 css 选择器一样。找到网页的导航id,从中解析出页数。

检查第 How to find HTML elements? section, tab Advanced 页。

例如http://www.carmudi.pk/cars/包含

中的总页数
<li class="total-pages"> of <strong>1036</strong> </li>

您可以使用 class total-pages 和解析文本来访问它。

如果您正在寻找适用于所有网站的通用解决方案,因为每个网站都有不同的 html 导航,每个网站都需要单独解析。