2016-03-23 5 views
0

Unten ist mein Code, der den Inhalt ausgibt, der unter Plot Registerkarte auf einer Wiki-Seite vorhanden ist, ich benutze getElementById und es wirft einige Ausnahmen, die ich unten eingefügt habe, kann jemand ändern, um zu arbeiten. Vielen Dank im Voraus.PHP Crawler Ausnahme

<?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'); 
?> 

Ausnahme ist:

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

Und was ist nicht zu verstehen? Es gibt kein Element mit der ID 'Plot' im DOM-Dokument. –

+0

@CharlotteDunois Die Seite https://en.wikipedia.org/wiki/I_Too_Had_a_Love_Story hat ID = Plot im Quellcode, die zurückgegeben werden muss, und es funktioniert für einige andere URLs. –

Antwort

0

ich versuchen, Ihren Code und es funktioniert und das Rück <span class="mw-headline" id="Plot">Plot</span>. Ich denke, Ihr Problem bei der Verwendung DOMDocument::loadHTMLFile mit @:

@$doc->loadHTMLFile($url); 

Da diese Methode

bool true on success or false on failure

kehrt Und es manchmal falsch zurückgeben (zum Beispiel 403 von wikipedia für viele Anfragen) und Ihr dom Element ist leer. In diesem Fall kann Ihr $element = $doc->getElementById($id); dieses Element nicht finden.

versuchen, Ihren Code zu ändern:

<?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 können für Ihr Skript nicht verfügbar (einige Websites Parser-Skripte blockieren). Versuchen Sie, curl zu verwenden, um status_code für Ihre Antwort zu erhalten

$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

Schwerwiegender Fehler: Uncaught-Ausnahme 'Exception' mit Nachricht 'Laden der https://en.wikipedia.org/wiki/I_Too_Had_a_Love_Story' fehlgeschlagen. C: \ xampp \ htdocs \ example2.php: 16 Stack-Trace: # 0 C: \ xampp \ htdocs \ example2.php (35): getElementByIdAsString() # 1 {main} geworfen in C: \ xampp \ htdocs \ example2.php in Zeile 16 –

+0

Ja, wikipedia kann für Ihr Skript nicht verfügbar sein (einige Seiten blockieren Parser-Skripte)). Versuchen Sie, curl zu verwenden, um status_code für Ihre Antwort zu erhalten $ 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-Zertifikat Problem, überprüfen Sie, ob das CA-Zertifikat in Ordnung ist. Details: Fehler: 14090086: SSL-Routinen: SSL3_GET_SERVER_CERTIFICATE: Zertifikatsprüfung fehlgeschlagen –