避免误导并继续孩子html_simple_dom

时间:2014-03-28 12:12:19

标签: php html api

当我这样做时:

if ($html->find(".infobox_v2", 0)->children(1)->children(0)->children(0)->href) {
            echo "infobox";
            infobox();

        }

找不到链接我收到此消息; 致命错误:在非对象上调用成员函数children() 我希望错误,但我想继续运行代码 谢谢!

仍然有相同的错误我附上代码:

 try {
       if ($html->find(".infobox_v2", 0)->children(1)->children(0)->children(0)->href) {
          echo "infobox";
          infobox();
       } else if (is_Object($html->find(".thumbinner", 0)) === true && is_Object($html->find(".thumbinner", 0)->children(0)) === true) {
          echo "thumbinner";
          thumbinner(); 
       } else if (is_Object($html->find(".ambox-image", 0))) {
              echo "ambos";
       } else {
          echo "errors<br />";
          $errors++;    
          $nomPersonatge[] = $nomDifunt;
       }
   }catch(Exception $e) {
                }
                echo "Fi ". $i. " part<br />";

感谢您的回答和不便。

以下是整个代码:

<?php
    include "../admin/include/configuracion.php";
//  include "../admin/include/database.php";
    include "../admin/include/funciones.php";
    include "../admin/include/tablas.php";
    include('simple_html_dom.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Documento sin título</title>
        <script type="text/javascript">
    </script>
    </head>

    <body>

<?php 
    mysql_conexion();
    $isObject = false;
    $errors = 0;
    $nomPersonatge = array();
    $nomDifunt = "";
    $query = "select nom from dpsinfo limit 10";

    $context = stream_context_create();
    stream_context_set_params($context, array('user_agent' => 'UserAgent/1.0'));

    $id_consulta = @mysql_query($query);
        for($i=0; $i<@mysql_num_rows($id_consulta); $i++) {
        //Bloc per agafar el nom del difunt i cambiar espais per "_"
        $nomDifunt = @mysql_result($id_consulta, $i, 0);
        $nomDifunt = str_replace (" ", "_", $nomDifunt);

        //Montem la url i la carreguem a la variable $html
        $url="http://es.wikipedia.org/wiki/".$nomDifunt;
        echo $url." <br />";

        $html = file_get_html($url, 0, $context);

        if(is_Object($html)) {
            //Monto el link de la pagina de dades del personatge
        $url2 = "http://commons.wikimedia.org";
        try {
                if ($html->find(".infobox_v2", 0)->children(1)->children(0)->children(0)->href && $html->find(".infobox_v2", 0)->children(1)->children(0)->children(0)) {
                    echo "infobox";
                    infobox();
            } else if (is_Object($html->find(".thumbinner", 0)) === true && is_Object($html->find(".thumbinner", 0)->children(0)) === true) {
                echo "thumbinner";
                thumbinner();   
            } else if (is_Object($html->find(".ambox-image", 0))) {
                echo "ambos";
            } else {
                echo "errors<br />";
                $errors++;  
                $nomPersonatge[] = $nomDifunt;
            }
            } catch(Exception $e) {
            $e->getMessage();
        }

        echo "Fi ". $i. " part<br />";

        } else {
            echo "<b>errors else</b><br />";
        $errors++;  
        $nomPersonatge[] = $nomDifunt;
        echo "Fi ". $i. " part<br />";
        }

    } //Tanca el primer for

function infobox() {
    global $html, $url2;
    $url2 .= $html->find(".infobox_v2", 0)->children(1)->children(0)->children(0)->href;
    $urlfinal = str_replace ( "Archivo:" ,"File:" , $url2);     //Substitueixo Archivo per File perquè si no, no trobarà res

    $html2 = file_get_html($urlfinal);      //Extraic tot el contingut de la nova pàgina generada
    $fotogran = $html2->find(".fullImageLink", 0)->children(0)->children(0)->src;

    echo $fotogran."<br />";
    echo $urlfinal."<br />";

}

function thumbinner() {
    global $html, $url2;
    $url2 .= $html->find("#mw-content-text", 0)->children(0)->children(0)->children(0)->href;
    $urlfinal = str_replace ( "Archivo:", "File:", $url2);

    $html2 = file_get_html($urlfinal);
    $fotogran = $html2->find(".fullImageLink", 0)->children(0)->children(0)->src;

    echo $fotogran."<br />";
    echo $urlfinal."<br />";
}



?>

    </body>
</html>

实际上,我正在使用此脚本崩溃此网页wikipedia

感谢您的关注。和Srry的不便。

1 个答案:

答案 0 :(得分:1)

如果您的主要概念是从WikiPedia获取所有图像,请使用以下代码(或) WikiPedia API

<?php

include('simple_html_dom.php');

set_time_limit(0);

function file_get_safe($link) {
    $content = stream_context_create(
        array(
            "http" => array(
                "timeout" => 30,
                "header"  =>
                    implode("\r\n",
                            array(
                                "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
                                "User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36",
                                "Accept-Language: en-US,en;q=0.8"
                            )
                    )
            )
        ));
    return @file_get_contents($link, FALSE, $content);
}

function wiki_big_image($image_link) {
    $output = NULL;
    if (filter_var($image_link, FILTER_VALIDATE_URL) !== FALSE) {
        $data = @str_get_html(file_get_safe($image_link));
        if (is_object($data)) {
            $big_image = $data->find("#file a[href*='upload']", 0)->href;
            $big_image = "http:{$big_image}";
            if (filter_var($big_image, FILTER_VALIDATE_URL))
                $output = $big_image;
        }
    }
    return $output;
}

function grab_wiki_images($wiki_word, $big_image = FALSE) {
    $output    = array();
    $big_image = $big_image === TRUE;
    if (is_string($wiki_word) && strlen($wiki_word)) {
        $wiki_word = str_replace(" ", "_", $wiki_word);
        $link      = "http://en.wikipedia.org/wiki/{$wiki_word}";
        $data      = @str_get_html(file_get_contents("wiki.dat"));
        if (is_object($data)) {
            foreach ($data->find("a[href*='\\/wiki\\/File:']") as $elm) {
                $small_image = "http://en.wikipedia.org{$elm->href}";
                if (@preg_match("/\\.(jpeg|jpg|png|svg)$/i", $small_image))
                    $output[] = $big_image ? wiki_big_image($small_image) : $small_image;
                unset($elm);
            }
        }
    }
    return $output;
}

$images = grab_wiki_images("Michael Jackson");

print_r($images);

?>