2016-03-25 2 views
0

私はgetElementByIdAsString( 'www.abebooks.com/9780143418764/Love-Story-)-)を使用するとうまく働いているPHPコードを使用して、Webサイトからコンテンツを抽出しようとしています。 Singh-Ravinder-0143418769/plp '、' synopsis ');wikiのウィキのためのPHPクローラー

しかし、同じコードを使用してwikipediaからコンテンツを取得すると、getElementByIdAsString( 'https://en.wikipedia.org/wiki/A_Brief_History_of_Time'、 'Summary');以下は

私のコードと私は後者one.Can誰かがIDに基づいて、事前に

感謝をWikipediaのコンテンツを抽出するために自分のコードを修正する使用するときに、私は取得しています例外です。

<?php 


function getElementByIdAsString($url, $id, $pretty = true) { 
    $doc = new DOMDocument(); 

    $ch = curl_init($url); 
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36'); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

    $result = curl_exec($ch); 


// var_dump($doc->loadHTMLFile($url)); die; 
error_reporting(E_ERROR | E_PARSE); 
    if(!$result) { 
     throw new Exception("Failed to load $url"); 
    } 
    $doc->loadHTML($result); 
    // Obtain the element 
    $element = $doc->getElementById($id); 

    if(!$element) { 
     throw new Exception("An element with id $id was not found"); 
    } 

    if($pretty) { 
     $doc->formatOutput = true; 
    } 

    // Return the string representation of the element 
    return $doc->saveXML($element); 
} 

//Here I am dispalying the output in bold text 
echo getElementByIdAsString('https://en.wikipedia.org/wiki/A_Brief_History_of_Time', 'Summary'); 

?> 

例外

Fatal error: Uncaught exception 'Exception' with message 'Failed to load http://en.wikipedia.org/wiki/A_Brief_History_of_Time' in C:\xampp\htdocs\example2.php:18 Stack trace: #0 C:\xampp\htdocs\example2.php(40): getElementByIdAsString() #1 {main} thrown in C:\xampp\htdocs\example2.php on line 18 

あなたの助けが

答えて

2

試みを追加するために非常にGREATFUL :-)次のようになります。

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 

更新後のコメントで議論:

<?php 

function getElementByIdAsString($url, $id, $pretty = true) { 
    $doc = new DOMDocument(); 

    $ch = curl_init($url); 
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36'); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 

    $result = curl_exec($ch); 

    error_reporting(E_ERROR | E_PARSE); 
    if(!$result) { 
     throw new Exception("Failed to load $url"); 
    } 
    $doc->loadHTML($result); 
    // Obtain the element 
    $element = $doc->getElementById($id); 

    if(!$element) { 
     throw new Exception("An element with id $id was not found"); 
    } 

    if($pretty) { 
     $doc->formatOutput = true; 
    } 

    $output = ''; 
    $node = $element->parentNode; 

    while(true) { 
     $node = $node->nextSibling; 
     if(!$node) { 
      break; 
     } 
     if($node->nodeName == 'p') { 
      $output .= $node->nodeValue; 
     } 
     if($node->nodeName == 'h2') { 
      break; 
     } 
    } 

    return $output; 
} 

//Here I am dispalying the output in bold text 
var_dump(getElementByIdAsString('https://en.wikipedia.org/wiki/A_Brief_History_of_Time', 'Summary')); 

おそらく、xPathsを使うこともできますし、レスポンス全体を使って正規表現で必要なものをカットすることもできます

+0

Liszka今回はエラーを返さないが、何もない空白のページがあります。 「 –

+0

を使用せずに特定のコンテンツを引き出す」このコードを実行すると、「 Summary "という基本的には、 getElementById(クロムコンソールで$( "#Summary")を使用するのと同じ効果があります。あなたは何をしようとしていますか? エコーを除いて、出力をvar_dumpしようとしますか? var_dump(getElementByIdAsString( 'https://en.wikipedia.org/wiki/A_Brief_History_of_Time'、 'Summary'))); –

+0

私は、[概要]タブでテキストを抽出したいと考えました。 –

関連する問題