2016-03-23 6 views
0

以下は、wikiページのPlotタブにあるコンテンツを出力するコードです。getElementByIdを使用しています。下に貼り付けたいくつかの例外がスローされています。 ありがとうございました。PHPクローラの例外

<?php 
/** 
* Downloads a web page from $url, selects the the element by $id 
* and returns it's xml string representation. 
*/ 
//Taking input 
if(isset($_POST['submit'])) /* i.e. the PHP code is executed only when someone presses Submit button in the below given HTML Form */ 
{ 
$var = $_POST['var']; // Here $var is the input taken from user. 
} 
function getElementByIdAsString($url, $id, $pretty = true) { 
    $doc = new DOMDocument(); 
    @$doc->loadHTMLFile($url); 

    if(!$doc) { 
     throw new Exception("Failed to load $url"); 
    } 

    // 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); 
} 

// call it: 
echo getElementByIdAsString('https://en.wikipedia.org/wiki/I_Too_Had_a_Love_Story', 'Plot'); 
?> 

例外は次のとおりです。

Fatal error: Uncaught exception 'Exception' with message 'An element with id Plot was not found' in C:\xampp\htdocs\example2.php:23 Stack trace: #0 C:\xampp\htdocs\example2.php(35): getElementByIdAsString() #1 {main} thrown in C:\xampp\htdocs\example2.php on line 23 
+0

そして何が分かりませんか? DOMドキュメントに 'Plot'というIDの要素はありません。 –

+0

@CharlotteDunois https://en.wikipedia.org/wiki/I_Too_Had_a_Love_Storyには、id = Plotが返される必要があるソースコードがあり、他のいくつかのURLのために働いています。 –

答えて

0

私はあなたのコードを試してみて、それが動作して<span class="mw-headline" id="Plot">Plot</span>を返します。この方法は、

bool true on success or false on failure

を返し、時にはそれが(多くの要求のためにウィキペディアから例403用)falseを返すと、あなたのDOM要素が空であるため

@$doc->loadHTMLFile($url); 

:私は@DOMDocument::loadHTMLFileを使用して、あなたの問題を考えます。この場合、$element = $doc->getElementById($id);はこの要素を見つけることができません。

がにあなたのコードを変更してください:

<?php 
/** 
* Downloads a web page from $url, selects the the element by $id 
* and returns it's xml string representation. 
*/ 
//Taking input 
if(isset($_POST['submit'])) /* i.e. the PHP code is executed only when someone presses Submit button in the below given HTML Form */ 
{ 
    $var = $_POST['var']; // Here $var is the input taken from user. 
} 
function getElementByIdAsString($url, $id, $pretty = true) { 
    $doc = new DOMDocument(); 
    $loadResult = @$doc->loadHTMLFile($url); 

    if(!$doc || !$loadResult) { 
     throw new Exception("Failed to load $url"); 
    } 

    // 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); 
} 

// call it: 
echo getElementByIdAsString('https://en.wikipedia.org/wiki/I_Too_Had_a_Love_Story', 'Plot'); 
?> 

Wkipediaは(いくつかのサイトがパーサスクリプトをブロックする)スクリプト使用できなくすることができます。 curlを使用してレスポンスのstatus_codeを取得しようとしてください

$url = 'en.wikipedia.org/wiki/I_Too_Had_a_Love_Story'; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$html = curl_exec($ch); 
$status_code = curl_getinfo($ch,CURLINFO_HTTP_CODE); 
+0

致命的なエラー:C:\ xampp \ htdocs \ example2.phpに「https://en.wikipedia.org/wiki/I_Too_Had_a_Love_Storyを読み込めませんでした」というメッセージが表示され、キャッチされない例外「例外」が発生しました:16スタックトレース:#0 C:\ xempp \ htdocs \ example2.php(35):getElementByIdAsString()#1メインのC:\ xampp \ htdocs \ example2.phpにスロー16 –

+0

はい、Wikipediaはあなたのスクリプトでは利用できません。 )。 curlを使用してレスポンスのステータスコードを取得してください。 $ url = 'https://en.wikipedia.org/wiki/I_Too_Had_a_Love_Story'; $ ch = curl_init(); curl_setopt($ ch、CURLOPT_URL、$ url); curl_setopt($ ch、CURLOPT_RETURNTRANSFER、1); $ html = curl_exec($ ch); $ status_code = curl_getinfo($ ch、CURLINFO_HTTP_CODE); –

+0

SSL証明書の問題で、CA証明書がOKであることを確認します。詳細:エラー:14090086:SSLルーチン:SSL3_GET_SERVER_CERTIFICATE:証明書の検証に失敗しました –