1

以前、グーグルでスクリプトを見つけて、メインクラスであるスクラップ目的で使用しました

私のamazon.phpで、私は次のスクリプトを書きました

include('scrape.php');
set_time_limit(0);


$ASIN       =   'B000GEM3RI';
$shipArray  =   shipingPrice($ASIN);
var_dump($shipArray);
print_r($shipArray);
echo $shipArray;



function shipingPrice($city){
        $shipArray = array();
        $scrape = new Scrape();
        $url = 'http://www.amazon.com/gp/offer-listing/'.$city.'/ref=dp_olp_new?ie=UTF8&condition=new';
        $scrape->fetch($url);
        $data = $scrape->removeNewlines($scrape->result);
        $data = $scrape->fetchBetween('<table cellspacing="0" cellpadding="0" width="100%" border="0"> <thead class="columnheader"><tr><th scope="col" class="price">Price + Shipping</th><th scope="col" class="condition">Condition</th><th scope="col" class="seller">Seller Information</th><th scope="col" class="readytobuy">Buying Options</th></tr></thead>','</table>',$data,true); 
        $rows = $scrape->fetchAllBetween('<tr','</tr>',$data,true);
        $i=0;$j=0;
        foreach ($rows as  $row){
            if($i!=0){
                if($i!=2){              
                    $record = array();
                    $cells = $scrape->fetchAllBetween('<td','</td>',$row,true);                 
                    $record['price'] = strip_tags($cells[0]);
                    
                    if(stristr($record['price'],'oz')===False && stristr($record['price'],'/')===False)
                    {
                        $listPrice=$scrape->fetchBetween('$',' +',$record['price']);
                    }else{
                        $listPrice=$scrape->fetchBetween('$',' (',$record['price']);
                    }
                    //print_r($listPrice);
                    if($listPrice==''){
                        $listPrice=$scrape->fetchBetween('$',' &',$record['price']);
                        $shipPrice='0';                     
                    }else{
                        $shipPrice=$scrape->fetchBetween('+ $','s',$record['price']);
                    }
                    $shipPrice= floatval($shipPrice);
                    //####                  
                    $sellerIdInfo = $cells[2];                                          $sellerIdArray=$scrape->fetchAllBetween('&marketplaceSeller=0&seller=','"><b>',$sellerIdInfo);                  
                    if(count($sellerIdArray)>1){
                        $sellerId=$sellerIdArray[0];
                    }else{
                        $temp = explode('"id',$sellerIdArray[0]);
                        $sellerId=$temp[0];                                         
                    }
                    //##
                    $sellerName =$scrape->fetchBetween('Seller:','Seller',$record['price']);                    
                    $sellerInfo=$scrape->fetchAllBetween('alt="','"',$cells[2],true);           
                    $sellerName=str_replace(array('alt="','"'),array('',''),$sellerInfo[0]);
                    if($sellerName!=""){        
                        //
                    }else{
                        $sellerName = $scrape->fetchBetween('<span class="sellerHeader">Seller:</span>','</b></a>',$cells[2],true);
                        $sellerName=str_replace("Seller:","",$sellerName);
                        $sellerName=$scrape->fetchBetween('<b>','</b>',$sellerName);            
                    }

                    array_push($shipArray,array('sellerName'=>$sellerName,'sellerId'=>$sellerId,'price'=>$listPrice,'shipPrice'=>$shipPrice));

                }
            }
            $i++;
        }
        return $shipArray;
}

このスクリプトのURLはamazonです

それをエコーすると、var_dump itまたはprint_r、空の配列が表示され、firebugを使用してページをチェックしましたが、コードではすべて問題ないように見えます

コードは問題ないのに、なぜページから何かにアクセスできるのか誰かに教えてもらえますか?

私を助けてくれてありがとう

編集:-

$this->result = curl_exec($ch);スクラップクラス関数fetch($ url)にreturnを追加することで、Pageが正常に取得されていることを確認しました...

編集-2:-

回答で提供されたアドバイスに取り組んだ後、それは試みました

$shipArray[]=array('sellerName'=>$sellerName,'sellerId'=>$sellerId,'price'=>$listPrice,'shipPrice'=>$shipPrice); 

私の関数では、それでも同じ空の配列

編集-3

次の機能を変更しました

function fetchBetween($needle1,$needle2,$haystack,$include=false){
        
        $position = strpos($haystack,$needle1);
        
        if ($position === false) { return ' it was null data'; }

echo $data;スクリプトファイルにエコーすると、

nullデータでした

が印刷されるため、このコード行が機能していないように見えます$position = strpos($haystack,$needle1);が、

私は正しいですか?

はいの場合、今何をしますか?

4

2 に答える 2

0

まだ定義されていない関数の結果として変数を定義するのはなぜですか?

$shipArray  =   shipingPrice($ASIN);
var_dump($shipArray);
print_r($shipArray);
echo $shipArray;

ShippingPrice関数はコードの下位にあります。IEshipArrayは空になります

于 2012-08-09T19:21:56.630 に答える
0

お奨め、私は正しかった、問題は私のコードになかった

オンライン

 $data = $scrape->fetchBetween('<table cellspacing="0" cellpadding="0" width="100%" border="0"> <thead class="columnheader"><tr><th scope="col" class="price">Price + Shipping</th><th scope="col" class="condition">Condition</th><th scope="col" class="seller">Seller Information</th><th scope="col" class="readytobuy">Buying Options</th></tr></thead>','</table>',$data,true);

余分なスペースがありborder="0"> <thead class、それが問題を引き起こしていました、一度、私はこれをからスペースを削除しました

<table cellspacing="0" cellpadding="0" width="100%" border="0"><thead class="columnheader">

私のコードは大丈夫になりました。

みんなありがとう

于 2012-08-10T16:37:50.860 に答える