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 检查了页面,对我来说,看起来我的代码中一切正常

有人可以告诉我为什么我可以从页面访问任何内容,尽管我的代码没问题吗?

谢谢你帮助我

编辑:-

通过在我的废料类函数 fetch($url) 中添加 return $this->result = curl_exec($ch);,我确保 Page 被成功检索...

EDIT-2:-

在按照答案中提供的建议工作后,它尝试了

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

在我的函数中,但仍然是相同的空数组

EDIT-3

我更改了以下功能

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

当我echo $data;在我的脚本文件中回显时,

这是空数据

已打印,所以看起来这行代码$position = strpos($haystack,$needle1);不起作用,

我对吗?

如果是,现在该怎么办?

4

2 回答 2

0

您为什么将 var 定义为尚未定义的函数的结果?

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

ShippingPrice 函数在代码中较低。IE shipArray 将为空

于 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 回答