我正在使用 PHP/CURL 构建一个(相对)简单的网络爬虫。这是我第一次使用 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);
}
?>
任何帮助将不胜感激。