CURL无法从站点正确获取数据

时间:2012-08-02 06:47:22

标签: php curl

我从网站www.example.com获取数据。数据采用类似于表格的结构,并且还有一个分页。在正确获取第一页数据和获取下一页数据时我运行我的代码{ {1}}。我知道总共没有3页。我的代码如下: -

forloop

问题是我第一次获取第一页数据3次。我想在一个表格中显示所有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); 的新手。

1 个答案:

答案 0 :(得分: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.Now通过编写以下代码我得到了我的解决方案: -

$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;