嗨!!各位前輩 大家好
小弟目前使用curl去做撈取資料的動作,但有時候撈取資料時卻會遇上只有撈到一個網站的資料,另一個網站則必須重新整理幾次後才撈的到,還請各位前輩指點迷津!!
這是小弟撈取資料的檔案
allaction.php
//第一個網站
$url= "$web_url1";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$output = curl_exec($ch);
curl_close($ch);
header('Content-type: text/html; charset=utf-8');
preg_match('/<span itemprop="price" content="\s*(.*?)\s*">\s*(.*?)\s*<\/span>/s', $output, $matches);
$price1 = strip_tags($matches[0]);
//第二個網站
$url= "$web_url2";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$output = curl_exec($ch);
curl_close($ch);
header('Content-type: text/html; charset=utf-8');
preg_match('/<span id="priceblock_ourprice" class="a-size-medium a-color-price">\s*(.*?)\s*<\/span>/s', $output, $matches);
$price = strip_tags($matches[0]);
$price2 = str_replace("¥"," ",$price);
不如這樣
<pre class="c" name="code">
$url= "$web_url1";
$output ="";
goGetIt( $url, $output );
function goGetIt( $url, &$output ){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$reTry = 1;
$http_code = 0;
while($http_code!=200 && $reTry<=5){
$output = curl_exec($ch);
$info = curl_getinfo($ch);
$http_code = $info["http_code"];
if ((int)$http_code!=200){
$reTry++;
sleep(3);
}
}
curl_close($ch);
}
header('Content-type: text/html; charset=utf-8');
preg_match('/<span itemprop="price" content="\s*(.*?)\s*">\s*(.*?)\s*<\/span>/s', $output, $matches);
$price1 = strip_tags($matches[0]);