【问题标题】:why code not working althoug it looks okay to me?为什么代码不工作,虽然它看起来对我没问题?
【发布时间】:2012-08-09 19:13:38
【问题描述】:

我之前在谷歌搜索时发现了一个脚本并将其用于报废目的,我的主要课程

在我的 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;
}

这个脚本的网址是amazon

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

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

谢谢你帮助我

编辑:-

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

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); 不起作用,

我说的对吗?

如果是,现在该怎么办?

【问题讨论】:

  • 你已经做了哪些调试?
  • 我试过 var_dump(),print_r(),我打开页面看看它是否存在,我看到了 html 节点树,看看我是否缺少一些标签 .. 像这样的东西
  • 为什么构造函数返回一些东西?
  • scrapping class不是我的,我在google的时候发现了它,我写了一个函数来使用它,我得到的返回true的逻辑是构造函数成功地启动了对象
  • @NewBee - 您是否检查过您的代码是否可以正常连接到站点?您返回的数据是否按照您的预期进行格式化?

标签: php arrays function screen-scraping amazon


【解决方案1】:

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

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

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

【讨论】:

  • 你可以在 PHP 代码中声明之前使用函数,预解析器会确保它们被正确声明。
  • 你能用数组做数组推送吗?你不会做 $shipArray = $shipArray + $otherArray 吗?在你的循环中?我撒谎说如果钥匙相同就行不通
  • @AlexReynolds 我对你的了解还不够,你能给我指点代码,还是只编辑特定的代码行?
  • array_push($shipArray,array('sellerName'=>$sellerName,'sellerId'=>$sellerId,'price'=>$listPrice,'shipPrice'=>$shipPrice))。我不确定您是否可以将一个数组推送到另一个多维数组上。如果你应该这样做 $shipArray[] = array('sellerName'=>$sellerName,'sellerId'=>$sellerId,'price'=>$listPrice,'shipPrice'=>$shipPrice)。
  • @NewBee 你至少从 $scrape->fetch($url) 获取数据吗?
【解决方案2】:

知道了,我是对的,问题不在于我的代码

上线

 $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"&gt; &lt;thead class,这是造成问题,有一次,我从

中删除了这个空间
<table cellspacing="0" cellpadding="0" width="100%" border="0"><thead class="columnheader">

我的代码没问题..

谢谢大家

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-05
    • 1970-01-01
    • 2020-11-01
    • 2013-07-09
    • 2021-01-10
    相关资源
    最近更新 更多