繁体   English   中英

PHP Web刮板代码导致内部服务器错误

[英]PHP Web scraper code causing an internal server error

我正在使用PHP / CURL构建(相对)简单的网络抓取工具。 这是我第一次使用PHP,我已经在ScraperWiki中测试了此代码,效果很好,但是我试图在自己的服务器上使用它,但它没有运行。 我知道脚本正在读取,因为如果删除了simple_html_dom include,则会收到错误消息。 但是,包括它在内时,出现500服务器错误。

真的不知道从哪里着手解决问题。 希望有人查看代码以查看是否有明显的错误吗? 目前,我只希望页面在屏幕上打印变量,这样我就知道它可以正常工作,然后将其连接到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);
}

?>

任何帮助将不胜感激。

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

但是,我确实注意到一件事,您正在检查if (isset($more1)) {当将$more变量设置为$the_html->find的结果时

通过在简单的html dom解析器中查看find方法的文档,如果找不到元素,它将返回null,因此检查应为if (!is_null($more1)) {

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM