2013-04-29 99 views
0

我正在使用PHP/CURL构建(相对)简单的网页刮板。这是我第一次使用PHP,我在ScraperWiki中测试了这段代码,它工作得很好,但我试图在我自己的服务器上使用它,但它没有运行。我知道脚本正在被读取,因为如果我删除了simple_html_dom,我会收到错误消息。但是当它包含时,我得到一个500服务器错误。PHP Web刮板代码导致内部服务器错误

不知道从哪里开始解决问题。希望有人查看代码,看看是否有任何明显的错误?目前我只想让页面在屏幕上打印变量,所以我知道它正常工作,然后我将它连接到mysql。所以,这只是我的服务器上的文件夹中,与simple_html_dom.php一起,和我转到里面有下面的代码www.domain.com/folder/index.php访问它:

<?php 
// Include simple html dom 
include('simple_html_dom.php'); 



    // Defining the basic cURL function 
    function curl($url) { 
     $ch = curl_init(); // Initialising cURL 
     curl_setopt($ch, CURLOPT_URL, $url); // Setting cURL's URL option with the $url variable passed into the function 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // Setting cURL's option to return the webpage data 
     $data = curl_exec($ch); // Executing the cURL request and assigning the returned data to the $data variable 
     curl_close($ch); // Closing cURL 
     return $data; // Returning the data from the function 
    } 


$allLinks = array(); 
$counter = 0; 

function nextPage($nextUrl){ 
    global $counter; 
    getLinks($nextUrl); 

} 

function getLinks($url){ // gets links from product list page 
    global $allLinks; 
    global $counter; 

    $html_content = curl($url); 
    $html = str_get_html($html_content); 

    foreach ($html->find("div.views-row a.imagecache-product_list") as $el) { 
     $url = $el->href . "\n"; 
     $allLinks[$counter] = 'http://www.uptherestore.com'; 
     $allLinks[$counter] .= $url; 
     $counter++; 
    } 

    $next = $html->find("li.pager-next a", 0); 
    if($next != false) $next = $next->href; 

    if (isset($next)) { 
     $nextUrl = 'http://www.uptherestore.com'; 
     $nextUrl .= $next; 
     nextPage($nextUrl); 
    }else{return;} 

} 

class Product{ //Creates an object class for products 
    public $name = ''; 
    public $infoLink = ''; 
    public $description = ''; 
    public $mainImage = ''; 
    public $moreImages1 = ''; 
    public $moreImages2 = ''; 
    public $moreImages3 = ''; 
    public $moreImages4 = ''; 
    public $price = ''; 
    public $designer= ''; 
} 


function getInfo($infoLink){ // Trawls the product pages for info 
    if(!(isset($i))) 
     {$i = 0;} 



    $the_content = curl($infoLink); 
    $the_html = str_get_html($the_content); 

    $productName = $the_html->find("#item_info h1", 0)->innertext; 

     $products[$productName] = new Product; 
     $products[$productName]->name = $productName; 
     $products[$productName]->infoLink = $infoLink; 
     $products[$productName]->designer = $the_html->find("#item_info h2", 0)->innertext; 
     $products[$productName]->description = $the_html->find("#item_info .product-body", 0)->innertext; //Might cause issues because there are multiple <p> tags in this div 
     $products[$productName]->mainImage = $the_html->find("#item_image .imagecache-product_item_default", 0)->src; 

     $more1 = $the_html->find(".extra_images", 0); 
     $more2 = $the_html->find(".extra_images", 1); 
     $more3 = $the_html->find(".extra_images", 2); 
     $more4 = $the_html->find(".extra_images", 3); 

     if (isset($more1)) { 
     $products[$productName]->moreImages1 = $more1->src; 
     } 
if (isset($more2)) { 
     $products[$productName]->moreImages1 = $more2->src; 
     } 
if (isset($more3)) { 
     $products[$productName]->moreImages1 = $more3->src; 
     } 
if (isset($more4)) { 
     $products[$productName]->moreImages1 = $more4->src; 
     } 
     $products[$productName]->price = $the_html->find(".price", 0)->innertext; 

// Store: $infoLink, $description, $mainImage, $moreImages, $price, $designer 
echo $products[$productName]->name . "\n"; 
echo $products[$productName]->description . "\n"; 
echo $i; 
$i++; 
} 



getLinks("http://www.uptherestore.com/department/accessories"); 

foreach ($allLinks as $key => $value) { 
    getInfo($value); 
} 

?> 

任何帮助将大大赞赏。

回答

1

如果您从中获得的唯一反馈是内部服务器错误,则很难确定可能会出现什么问题。我会尝试放入一些error_log调用或echo/print来找出停止运行的点。

有一件事我注意到,然而,就是你正在检查if (isset($more1)) { 当您设置$more变量的$the_html->find

结果从看文档在简单的HTML DOM解析器find方法,如果它找不到一个元素,它将返回null,因此检查应该是if (!is_null($more1)) {

您可以看到是否解决了问题,但是如果不是,我建议将某些日志记录或检查服务器/ php日志。

+0

不幸的是,它没有写任何东西到日志中,我尝试在一些'error_log(“你有这么远”,0)中加入;'但无济于事。该页面大约需要10秒来加载/给我500错误,也许这是某种内存问题? – Jascination 2013-04-29 12:10:16

+0

我相信问题可能出现在'isset($ next)'部分。首先,总是会设置'$ next'(出于与$ more'变量相同的原因)。其次,如果下一页链接到您已经访问过的页面,这可能会导致无限递归问题。为避免这种情况,可能需要进行检查,因为我目前没有看到一个 – Pudge601 2013-04-29 13:15:15