2017-12-30 9 views
0

문서 파서의 결과를 반향 할 수 있습니까? 아니면 결과를 표시하기 위해 먼저 배열을 만들어야합니까? 어쨌든 코드를 실행할 때 아무 것도 나타나지 않고 (출력 또는 오류 없음) 두 가지 방법을 모두 시도했습니다. 아마도 사이트 문제 일 수 있지만 몇 가지 다른 시도하고 동일한 결과를 얻을.PHP 문서 파서의 결과 표시

<?php 
$ebayquery ='halo'; 
$ebayhtml = 'https://www.ebay.com/sch/i.html_from=R40&_trksid=p2380057.m570.l1311.R6.TR12.TRC2.A0.H0.X.TRS0&_nkw=' . $ebayquery . '&_sacat=0'; 
$ebayresults = array(); 

$document = new \DOMDocument('1.0', 'UTF-8'); 
$internalErrors = libxml_use_internal_errors(true); 
$document->loadHTML($ebayhtml); 
libxml_use_internal_errors($internalErrors); 
$xpath = new DOMXpath($document); 
$links = $xpath->query('//h3[@id="lvtitle"]/a'); 

foreach($links as $a) { 
    echo $a->nodeValue; 
} 
?> 

답변

1

코드에 몇 가지 문제점이 있습니다. 첫째, loadHTML()은 파일 이름이나 URI가 아닌 HTML 문자열을 사용한다는 것입니다. 먼저 웹 페이지를 읽고 패스해야합니다 (여기서는 file_get_contents()을 사용했습니다).

둘째, XPath는 id 속성이 lvtitle 인 <h3> 태그를 찾고 있었지만 클래스 속성이 lvtitle 인 인스턴스 만 있습니다. 이 대신 XPath 표현식을 업데이트했습니다. 두 번째 문제에 대한 솔루션이 마치 마법처럼 일 동안 실행될 때, URL이 검색 쿼리에서 다시 한 번 웹 사이트에 도착, 두 번 사용하고 있기 때문에

$ebayquery ='halo'; 
$ebayhtml = 'https://www.ebay.com/sch/i.html_from=R40&_trksid=p2380057.m570.l1311.R6.TR12.TRC2.A0.H0.X.TRS0&_nkw=' . $ebayquery . '&_sacat=0'; 
$ebayresults = array(); 

$document = new \DOMDocument('1.0', 'UTF-8'); 
$internalErrors = libxml_use_internal_errors(true); 
$ebayhtml = file_get_contents($ebayhtml); 
$document->loadHTML($ebayhtml); 
libxml_use_internal_errors($internalErrors); 
$xpath = new DOMXpath($document); 
$links = $xpath->query('//h3[@class="lvtitle"]/a'); 
print_r($links); 
foreach($links as $a) { 
    echo $a->nodeValue.PHP_EOL; 
} 
+0

은, 첫 번째는 다소 결함이있다. 결과는 "from = R40 & trksid ........"이 검색 상자에 입력 된 경우에 표시됩니다. – Capattax

+0

또한 결과리스트 앞에'DOMNodeList Object ([length] => 48)'없이 결과를 어떻게 출력 할 수 있습니까? – Capattax

+1

어떻게 결과 페이지를 가져와야하는지 잘 모르겠지만 중요한 부분은 URI가 아닌 페이지를 가져 오는 결과를 전달하는 것입니다. 여분의 출력을 제거하려면 코드에서 print_r을 제거하십시오. –