2013-01-25 2 views
0

나는 automoto 사이트에 대한 스크래퍼를 만들고 처음에는 모든 제조품을 얻고 각각의 모델에 대한 모든 링크는 제조하지만 아래의 코드는 목록의 첫 번째 모델 만 얻습니다. 왜?링크를 추출하는 Xpath

<?php 

$dom = new DOMDocument(); 
@$dom->loadHTMLFile('http://www.auto-types.com'); 
$xpath = new DOMXPath($dom); 
$entries = $xpath->query("//li[@class='clearfix_center']/a/@href"); 
$output = array(); 
foreach($entries as $e) { 
    $dom2 = new DOMDocument(); 
    @$dom2->loadHTMLFile('http://www.auto-types.com' . $e->textContent); 
    $xpath2 = new DOMXPath($dom2); 
    $data = array(); 
    $data['newLinks'] = trim($xpath2->query("//div[@class='modelImage']/a/@href")->item(0)->textContent); 

    $output[] = $data; 
} 

echo '<pre>' . print_r($output, true) . '</pre>'; 

?> 

그래서 내가 얻을 필요가 : 벤츠/100 ...

도와주세요, 그래서 메르세데스/100, 메르세데스/200, 벤츠/300하지만 지금은 내 스크립트 내가 첫 번째 링크를 얻을

답변

1

첫 번째 항목을 가져 오는 대신 결과를 반복해야합니다.

$items = $xpath2->query("//div[@class='modelImage']/a/@href"); 
$links = array(); 
foreach($items as $item) { 
    $links[] = $item->textContent; 
} 
$data['newLinks'] = implode(', ', $links); 
관련 문제