我正在使用PHP / CURL构建一个(相对)简单的Web scraper。这是我第一次使用PHP,我已经在ScraperWiki中测试了这个代码并且它运行得很好但是我试图在我自己的服务器上使用它并且它没有运行。我知道正在读取脚本,因为如果删除simple_html_dom,我会收到错误消息。但是如果包含它,我会收到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);
}
?>
任何帮助都会非常赞赏。
答案 0 :(得分:1)
如果您从中获得的唯一反馈是内部服务器错误,则很难确定可能出现的问题。我会尝试输入一些error_log调用或echo / print来查明它何时停止运行。
然而,我注意到的一件事是你正在检查if (isset($more1)) {
将$more
变量设置为$the_html->find
从简单的html dom解析器中查看find方法的docs,如果找不到元素,它将返回null,因此检查应为if (!is_null($more1)) {
您可以看看是否能解决问题,但如果没有,我建议您进行一些日志记录或检查服务器/ php日志。