2013-12-09 3 views
0

개인 프로젝트를 만들려고 노력하고 있습니다. 그러나 Simple HTML DOM 클래스를 사용할 때는 약간의 어려움이 있습니다.PHP 간단한 HTML DOM 긁힌 외부 URL

내가하고 싶은 것은 웹 사이트를 긁어내어 모든 콘텐츠를 검색하고 내부 HTML로 특정 클래스와 일치시키는 것입니다.

내 코드는 지금까지 있습니다 :

<?php 
    error_reporting(E_ALL); 
    include_once("simple_html_dom.php"); 
    //use curl to get html content 
    $url = 'http://www.peopleperhour.com/freelance-seo-jobs'; 

    $html = file_get_html($url); 

    //Get all data inside the <div class="item-list"> 
    foreach($html->find('div[class=item-list]') as $div) { 
    //get all div's inside "item-list" 
    foreach($div->find('div') as $d) { 
    //get the inner HTML 
    $data = $d->outertext; 
    } 
    } 
print_r($data) 
    echo "END"; 
    ?> 

나는이 함께 얻을 모든 "END"아무것도 모든 출력에 빈 페이지입니다.

+1

[웹 페이지 내용 고르기] (http://stackoverflow.com/questions/584826/scrape-web-page-contents) –

답변

0

난 당신이 당신에게이 같은 (당신은 적절한 스타일 시트를 추가하는 경우, 그것은 잘 표시됩니다)

enter image description here

을 줄 것이다이

$url = 'http://www.peopleperhour.com/freelance-seo-jobs'; 
$html = file_get_html($url); 
foreach ($html->find('div.item-list div.item') as $div) { 
    echo $div . '<br />'; 
}; 

같은 것을 할 수 있습니다 생각

+0

완벽한! 예상대로 작동합니다. 클래스를 정의 할 때'div [class = item-list]'와 같이 어떻게 작동하지 않을까요? – MikeF

+0

아마도 item-list를 인용 할 필요가있을 것입니다. – pguardiario

1

$ 반복 변수마다 다른 값이 할당 된 것으로 보입니다. 대신 다음을 시도하십시오.

$data = ""; 
foreach($html->find('div[class=item-list]') as $div) { 
    //get all divs inside "item-list" 
    foreach($div->find('div') as $d) { 
     //get the inner HTML 
     $data .= $d->outertext; 
    } 
} 
print_r($data) 

도움이되기를 바랍니다.

+0

네가 맞다. 나는 대답을 편집 할 것이다. –

+0

불행히도 이것은 여전히 ​​작동하지 않습니다. 그러나 Sheikh의 대답처럼 수업을 정의 할 때 완벽하게 작동합니다. – MikeF