2012-07-12 3 views
3

나는 단순한 html-dom을 사용하여 특정 사이트의 제목을 긁어 모으고 있습니다.왜이 사이트에서 제목을 긁적니까?

<?php 

include('simple_html_dom.php'); 

$html = file_get_html('http://www.pottermore.com/'); 

foreach($html->find('title') as $element) 
     echo $element->innertext . '<br>'; 

?> 

내가 시도한 다른 사이트, 예를 들어 apple.com.

그러나 pottermore.com을 입력하면 아무 것도 출력하지 않습니다. Pottermore는 그것에 플래시 요소를 가지고있다. 그러나 나는 타이틀을 긁어 모으려고 노력하는 홈 스크린은 플래시가 없다, 그냥 html이다.

+4

을 'var_dump ($ html-> find ('title'));'출력은 무엇입니까? 당신은 OP에있는 내용을 넣을 수 있습니까? (너무 길지 않은 경우, 때로는 단순한 html-dom의 결과물입니다) – h2ooooooo

+2

아마도 pottermore는 긁어 모으기 방지 장치가 있으며 html이 아닌 다른 것을 돌려 받고 있습니다. –

+4

아마도 사이트에서 사용자 에이전트 문자열 등을 확인하고 있습니까? HTML이 제대로 다운로드되고 있습니까? – Brad

답변

1

이 나를 위해 작동합니다 :)

$url = 'http://www.pottermore.com/'; 
$html = get_html($url); 
file_put_contents('page.htm',$html);//just to test what you have downloaded 
echo 'The title from: '.$url.' is: '.get_snip($html, '<title>','</title>'); 

function get_html($url) 
{ 
    $ch = curl_init(); 
    $header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,"; 
    $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"; 
    $header[] = "Cache-Control: max-age=0"; 
    $header[] = "Connection: keep-alive"; 
    $header[] = "Keep-Alive: 300"; 
    $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"; 
    $header[] = "Accept-Language: en-us,en;q=0.5"; 
    $header[] = "Pragma: "; //browsers keep this blank. 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows;U;Windows NT 5.0;en-US;rv:1.4) Gecko/20030624 Netscape/7.1 (ax)'); 
    curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate'); 
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20); 
    curl_setopt($ch, CURLOPT_AUTOREFERER, true); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
    curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIE); 
    curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE); 
    $result = curl_exec ($ch); 
    curl_close ($ch); 
    return($result); 
} 

function get_snip($string,$start,$end,$trim_start='1',$trim_end='1') 
{ 
    $startpos = strpos($string,$start); 
    $endpos = strpos($string,$end,$startpos); 

    if($trim_start!='') 
    { 
     $startpos += strlen($start); 
    } 
    if($trim_end=='') 
    { 
     $endpos += strlen($end); 
    } 
    return(substr($string,$startpos,($endpos-$startpos))); 
} 
+0

cURL +는 실제 문제를 해결하지 못하고 페이지 제목을 찾는 것입니다. – brezanac

+0

죄송합니다, 지금 올바르게 질문하셨습니다 : (|) - 위 편집을 참조하십시오 –

+0

감사합니다! 나는 아직도 조금 혼란 스럽지만, get_snip이 실제로 문제를 해결하기 위해 무엇을하고 있는지에 관해서는 혼란 스럽다. – Alex

0

기능 file_get_html는 내부적으로 file_get_contents를 사용합니다. 이 함수는 URL에서 데이터를 가져올 수 있지만 이렇게하려면 사용자 에이전트 문자열을 보냅니다.

기본적으로이 문자열은 비어 있습니다. 일부 웹 서버는이 사실을 사용하여 비 브라우저가 데이터에 액세스하고이를 금지하도록 선택합니다.

php.ini에서 user_agent을 설정하여 전송되는 사용자 에이전트 문자열을 제어 할 수 있습니다. 또는 시도해 볼 수도 있습니다.

ini_set('user_agent','UA-String'); 

'UA-String'으로 설정하십시오.

1

다른 사용자의 의견을 확인하기 만하면 사용자 에이전트 문자열을 보내지 않으면이 사이트에서 금지 된 403을 보냅니다.

이 나를 위해 일 추가 :

사용자 에이전트 : 모질라/5.0 (윈도우, U, 윈도우 NT 5.0; EN-US; RV : 1.4) 게코/20030624 넷스케이프/7.1 (도끼)