首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么代码不起作用--尽管我觉得它还好?

为什么代码不起作用--尽管我觉得它还好?
EN

Stack Overflow用户
提问于 2012-08-09 19:13:38
回答 2查看 258关注 0票数 1

我以前在googling时找到了一个脚本,并将它用于我的主要类--报废目的。

在我的amazon.php中,我编写了以下脚本

代码语言:javascript
复制
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为亚马逊

当我回显它、var_dump it或print_r时,会显示一个空数组,我使用firebug检查页面,在我看来,在我的代码中一切正常。

有人能告诉我,为什么我可以访问页面中的任何东西,尽管我的代码还好吗?

谢谢你帮我

编辑:-

通过在我的报废类函数fetch($url)中添加返回$url,我已经确保了页面的成功检索.

编辑-2:-

在按照答复中的建议进行工作之后,它尝试了

代码语言:javascript
复制
$shipArray[]=array('sellerName'=>$sellerName,'sellerId'=>$sellerId,'price'=>$listPrice,'shipPrice'=>$shipPrice); 

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

编辑-3

我更改了以下函数

代码语言:javascript
复制
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);不工作,

我说的对吗?

如果是,现在该怎么办?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-08-10 16:37:50

明白了,我是对的,我的代码没有问题

在线

代码语言:javascript
复制
 $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,这造成了问题,有一次,我把这个空间从

代码语言:javascript
复制
<table cellspacing="0" cellpadding="0" width="100%" border="0"><thead class="columnheader">

我的代码没问题..。

感谢每个人

票数 0
EN

Stack Overflow用户

发布于 2012-08-09 19:21:56

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

代码语言:javascript
复制
$shipArray  =   shipingPrice($ASIN);
var_dump($shipArray);
print_r($shipArray);
echo $shipArray;

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11890384

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档