【问题标题】:simple html dom 404 error简单的 html dom 404 错误
【发布时间】:2014-05-18 21:28:06
【问题描述】:

大家好,我有这个代码:

<?php

include './simple_html_dom.php';

//this link exists
$teste = new simple_html_dom("http://www.btolinux.com.br/");
echo $teste->original_size."<br>";
if($teste->original_size !== 0){
    $teste->find("html");
}

//At this time I'm forcing get a 404 error link.
$teste = new simple_html_dom("http://www.btolinux.com.br/error/");
echo $teste->original_size."<br>";
if($teste->original_size !== 0){
    $teste->find("html");
}

//Now I'm get the correct link again. Why the error persists?
$teste = new simple_html_dom("http://www.btolinux.com.br/");
echo $teste->original_size."<br>";
if($teste->original_size !== 0){
    $teste->find("html");
}

?>

因此,在我的代码中,当我发现一个 404 错误链接时,在使用同一类创建的下一个对象中,错误仍然存​​在。 我该如何解决这个问题?

要尝试此代码,请获取 http://sourceforge.net/projects/simplehtmldom/files/ 中的 Simple_dom_php

【问题讨论】:

  • 能否详细解释一下
  • 此代码将此错误返回给我:“致命错误:在第 1113 行的 /var/www/html/crawler/simple_html_dom.php 中的非对象上调用成员函数 find()”我的 PHP 版本是 5.5.9-1ubuntu4 (cli)

标签: php web-crawler simple-html-dom


【解决方案1】:

尝试@以避免警告,如下所示:

<?php
//CORRECT
$url = "http://www.btolinux.com.br/";
$html = @file_get_contents($url);
if ($html!='') {
    $teste = new simple_html_dom($url);
    echo 'SIZE: '.$teste->original_size."<br>";    
}


//ERROR
$url = "http://www.btolinux.com.br/error/";
$html = @file_get_contents($url);
if ($html!='') {
    $teste = new simple_html_dom($url);
    echo 'SIZE: '.$teste->original_size."<br>";    
}
?>

【讨论】:

  • 嗯,这是个好主意,但是当我使用此代码时:` $url = "btolinux.com.br"; $html = @file_get_contents($url); if ($html!='') { $teste = new simple_html_dom($url); echo 'SIZE: '.$teste->original_size."
    "; $teste->find("html"); `错误再次出现:致命错误:在第1113行的/var/www/html/crawler/simple_html_dom.php中的非对象上调用成员函数find()
  • 你需要 echo 或 print_r $teste->find("html")... 但尽量避免它,因为它会 print_r 一个巨大的对象,你的服务器可能会崩溃,如果你得到非对象错误尝试使用 if(is_object($teste)){//do your code}
  • 问题是,在得到一个 404 错误之后,该类再也没有转向一个有效的对象......
  • 我做了一些修改,这段代码解决了我的问题...谢谢
  • 现在代码:$html = @file_get_contents($url); if ($html!='') { $html1 = new simple_html_dom($html);
【解决方案2】:

试试这个代码:

<?php
require_once("./simple_html_dom.php");          # simplehtmldom.sourceforge.net

$url="http://www.btolinux.com.br/";
$url404="http://www.btolinux.com.br/error/";

$teste = @file_get_html($url);
if ($teste && $teste->original_size) {
    echo $teste->original_size."<br>\n";
    $html = $teste->find("html");
}

$teste = @file_get_html($url404);
if ($teste && $teste->original_size) {
    echo $teste->original_size."<br>\n";
    $html = $teste->find("html");
}

$teste = @file_get_html($url);
if ($teste && $teste->original_size) {
    echo $teste->original_size."<br>\n";
    $html = $teste->find("html");
}
?>

我的输出是:

61206<br>
61206<br>

【讨论】:

    【解决方案3】:

    您是否尝试过先使用 file_get_contents()? 例如,如果您使用代码:

    <?php
    print_r(file_get_contents("http://www.btolinux.com.br/error/"));
    ?>
    

    你会看到有错误,然后你可以try-catch它。

    【讨论】:

    • 我用过,但出现以下消息:警告:file_get_contents(btolinux.com.br/error):打开流失败:HTTP 请求失败! HTTP/1.1 404 Not Found in /var/www/html/crawler/simple_html_dom.php on line 75 注意:尝试在第 18 行的 /var/www/html/crawler/TestCrawler.php 中获取非对象的属性 注意:试图在第 19 行的 /var/www/html/crawler/TestCrawler.php 中获取非对象的属性 致命错误:在 /var/www/html/crawler/ 中的非对象上调用成员函数 find()第 20 行的 TestCrawler.php
    【解决方案4】:

    建议:

    $teste = new simple_html_dom();
    $teste->load_file("http://www.btolinux.com.br/");
    ...
    

    ...或者...

    $teste = file_get_html("http://www.btolinux.com.br/");
    

    【讨论】:

    • 嗨,当我使用这个替代品时,会出现以下错误消息:
    • 嗨,当我使用这个替代方案时,会出现以下错误消息:警告:file_get_contents(btolinux.com.br/error):打开流失败:HTTP 请求失败! HTTP/1.1 404 Not Found in /var/www/html/crawler/simple_html_dom.php on line 75 注意:尝试在第 18 行的 /var/www/html/crawler/TestCrawler.php 中获取非对象的属性 注意:试图在第 19 行的 /var/www/html/crawler/TestCrawler.php 中获取非对象的属性 致命错误:在 /var/www/html/crawler/ 中的非对象上调用成员函数 find()第 20 行的 TestCrawler.php
    猜你喜欢
    • 2013-05-28
    • 1970-01-01
    • 1970-01-01
    • 2012-06-27
    • 2018-02-23
    • 1970-01-01
    • 2023-03-04
    • 2016-03-11
    • 1970-01-01
    相关资源
    最近更新 更多