2014-07-13 3 views
1

저는 curl과 xpath에서 조금 새로운 편이므로 여전히 in과 out을 배우고 있습니다. 스크레이퍼를 작성했지만 배열을 통해 스크랩 한 데이터를 표시하려고 시도하면 아무 것도 나타나지 않습니다. 그래서 내 코드에 어떤 문제가 있습니까?Scraper가 빈 배열을 반환합니다.

<?php 

ini_set("display_errors", "1"); 
error_reporting(-1); 
error_reporting(E_ERROR); 
libxml_use_internal_errors(true); 

//Basic Function 
function get_url_contents($url, $timeout = 10, $userAgent = 'Mozilla/5.0(Macintosh; U; Intel Mac OS X 10_5_8; en-US)AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.215 Safari/534.10'){ 
    $rawhtml = curl_init();//handler 
    curl_setopt($rawhtml, CURLOPT_URL,$url);//url 
    curl_setopt($rawhtml, CURLOPT_RETURNTRANSFER, 1);//return result as string rahter than direct output 
    curl_setopt($rawhtml, CURLOPT_CONNECTTIMEOUT,$timeout);//set timeout 
    curl_setopt($rawhtml, CURLOPT_USERAGENT,$userAgent);//set user agent 
    $output = curl_exec($rawhtml);//execute curl call 
    curl_close($rawhtml);//close connection 

    if(!$output){ 
     return -1;//if nothing obtained, return -1 
    } 
    return $output; 
} 

//get raw html 
$html_string = get_url_contents("http://www.beursgorilla.nl/fonds-informatie.asp?naam=Aegon&cat=koersen&subcat=1&instrumentcode=955000020");//url here 
//load HTML into DOM object 
//ref http://www.php.net/manual/en/domdocument.loadhtml.php 
//note html does not have to be well fpr,ed with this function 

$dom_object = new DOMDocument(); 
@$dom_object->loadHTML($html_string); 

//perform Xpath queries on DOM 
//ref http://www.php.net/manual/en/domxpath.query.php 

$xpath = new DOMXPath($dom_object); 

//perform Xpath query 
//use any specfic property to narrow focus 

$nodes = $xpath->query("//table[@class='maintable']/tbody/tr[4]/td[2]/table[@class='koersen_tabel']/tbody/tr[2]/td[@class='koersen_tabel_midden']"); 

//setup some basic variables 

$i = -1; //$i = counter 

//when process nodes as below, cycling trough 
//but not grabbing data from the header row of the table 

$result = array(); 

//preform xpath subqueries to get numbers 

foreach($nodes as $node){ 
    $i++; 
    //using each 'node' as the limit for the new xpath to search within 
    //make queries relative by starting them with a dot (e.g. ".//...") 

    $details = $xpath->query("//table[3]/tbody/tr/td[1]/table[@class='fonds_info_koersen_links']/tbody/tr[1]/td[2]", $node); 
    foreach($details as $detail){ 
     $result[$i][''] = $detail->nodeValue; 
    } 

    $details = $xpath->query("//table[3]/tbody/tr/td[1]/table[@class='fonds_info_koersen_links']/tbody/tr[4]/td[2]", $node); 
    foreach($details as $detail){ 
     $result[$i][''] = $detail->nodeValue; 
    } 

    if(curl_errno($rawhtml)){ 
     echo 'Curl error: ' . curl_error($rawhtml); 

     print'<pre>'; 
     print_r($result); 
     print '</pre>'; 
    } 
} 

?> 

xpath 쿼리는 Chrome의 요소 검사기를 통해 확인되었으며 올바른 것으로 보입니다. 나는 코드에 무엇이 잘못되었는지 정말로 모른다.

+0

스크립트에서 무슨 일이 벌어지고 있는지 보려면'echo'를 더 사용하십시오 - 모든 변수를 출력하고'if/foreach'가 실행됩니다. – furas

답변

1

이 코드 라인은 어떻게됩니까?

$result[$i][] = $detail->nodeValue; 

내가 내 크롤러를 다시 작성하고 PHP 간단한 HTML DOM 파서 사용했다

0

(대괄호를 보면) :

$result[$i][''] = $detail->nodeValue; 

이 같이해서는 안된다. 이건 내 문제를 해결, 모든게 지금 :).

관련 문제