以下は、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
そして何が分かりませんか? DOMドキュメントに 'Plot'というIDの要素はありません。 –
@CharlotteDunois https://en.wikipedia.org/wiki/I_Too_Had_a_Love_Storyには、id = Plotが返される必要があるソースコードがあり、他のいくつかのURLのために働いています。 –