1

我从一个站点获取数据www.example.com。数据在类似结构的表格中,并且还有一个分页。我正确获取第一页数据并获取下一页数据我在运行我的代码。forloop我知道总共没有页面将是3.我的代码如下:-

$url = "http://www.example.com/browseall";

$ch = curl_init();

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_URL,$url);

$output = curl_exec($ch);

$html = new simple_html_dom();
$html->load_file($url);
foreach($html->find('div.full_listing_pager') as $pages)
{
    $page = $pages->children(2)->plaintext;
}
curl_close($ch);
$limit = $page+1;

echo "limit--->".$limit;


echo "<table border=1>";
echo "<tr>";
echo "<th>Listing Id </th>";
echo "<th>Free Km Allowed</th>";
echo "<th>Free Days allowed</th>";
echo "<th>Driver requirements</th>";
echo "<th>Owner comments</th>";
echo "</tr>";


for($i=1;$i<$limit+1;$i++) //$limit =3(no of pages)
{
    $url=urlencode('http://www.example.com.au/browseall?browse_filter[from_city]=0&browse_filter[to_city]=0&browse_filter[car]=0&browse_filter[by_date]=0&page='.$i);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, $url);

    $output = curl_exec($ch);

    foreach($html->find('table.full_listings_table tbody tr.more_info_second') as $div)
    {
        $str = "<tr>";

        $data = $div->find('td p b',0)->plaintext;
        $str .="<td>".$data."</td>";

        $data = $div->find('td div b',0)->plaintext;
        $str .="<td>".$data."</td>";

        $data = $div->find('td br b',0)->plaintext;
        $str .="<td>".$data."</td>";

        $data = $div->find('td div',0)->plaintext;
        $dataLen = strlen($data);
        $temp = "Driver requirements:";
        $tempLen = strlen($temp);
        $pos = strpos($data,$temp,0);

        $sum = $pos + $tempLen;
        $finalData = substr($data,$sum,$dataLen-$sum);

        $str .="<td>".$finalData."</td>"; 

        $data = $div->find('td div',2)->plaintext;
        $data = str_replace("Owner comments:"," ",$data);
        $str .="<td>".$data."</td>"; 

        echo $str."</tr>";
    }
}
echo "</table>";
curl_close($ch);

问题是我获取第一页数据 3 次。我想要一个表中的所有 3 页数据。我的代码有问题吗?请帮助我,我是新手cURL

4

1 回答 1

1

上帝终于找到了解决办法……我写的真是个白痴

$url=urlencode('http://www.example.com.au/browseall?browse_filter[from_city]=0&browse_filter[to_city]=0&browse_filter[car]=0&browse_filter[by_date]=0&page='.$i);

不需要使用 urlencode。现在通过编写以下代码,我得到了我的解决方案:-

$url='http://www.example.com.au/browseall?browse_filter[from_city]=0&browse_filter[to_city]=0&browse_filter[car]=0&browse_filter[by_date]=0&page='.$i;
于 2012-08-02T07:20:43.340 回答