【问题标题】:simple_html_dom.php parser not workingsimple_html_dom.php 解析器不工作
【发布时间】:2023-03-30 16:48:01
【问题描述】:

我下载了 simple_html_dom.php 文件并将其上传到我的网络服务器,我立即用一个简单的脚本对其进行了测试,但它不起作用,或者至少我认为它不起作用,因为它没有输出任何东西.

这是脚本:

<?php
require('simple_html_dom.php');

// Retrieve the DOM from a given URL
$html = file_get_html('http://davidwalsh.name/');

// Find all "A" tags and print their HREFs
foreach($html->find('a') as $e) {
    echo $e->innertext . '<br>';
}
?>

我从这个站点 http://davidwalsh.name/php-notifications 获得了脚本,并且在其他站点上也出现了相同的内容,所以我不明白为什么它不会输出任何内容。

我的猜测是脚本无法从其他网站检索任何数据,就像我在这里遇到的问题:Retrieving cross-domain data php

如果是这样,有什么办法可以避免这个问题吗?

在 BIno Carlos 对我之前链接的问题的回答中,他说“这并不是真正的跨域问题,因为您是从服务器而不是浏览器加载数据”,因此是否有例如加载数据的方法来自浏览器?

所以,正如 user868766 在他的回答中所建议的那样,我尝试了两个没有报告任何错误的 ini_set 方法,所以看起来脚本似乎工作得很好。当我在 $html 上尝试 print_r() 方法时,它输出以下内容:

simple_html_dom Object ( [root] => simple_html_dom_node Object ( [nodetype] => 5 [tag] => root [attr] => Array ( ) [children] => Array ( ) [nodes] => Array ( ) [parent] => [_] => Array ( [0] => -1 [1] => 1 ) [dom:simple_html_dom_node:private] => simple_html_dom Object *RECURSION* ) [nodes] => Array ( [0] => simple_html_dom_node Object ( [nodetype] => 5 [tag] => root [attr] => Array ( ) [children] => Array ( ) [nodes] => Array ( ) [parent] => [_] => Array ( [0] => -1 [1] => 1 ) [dom:simple_html_dom_node:private] => simple_html_dom Object *RECURSION* ) ) [callback] => [lowercase] => 1 [pos:protected] => 0 [char:protected] => [size:protected] => 0 [cursor:protected] => 1 [parent:protected] => simple_html_dom_node Object ( [nodetype] => 5 [tag] => root [attr] => Array ( ) [children] => Array ( ) [nodes] => Array ( ) [parent] => [_] => Array ( [0] => -1 [1] => 1 ) [dom:simple_html_dom_node:private] => simple_html_dom Object *RECURSION* ) [token_blank:protected] => [token_equal:protected] => =/> [token_slash:protected] => /> [token_attr:protected] => > [self_closing_tags:protected] => Array ( [img] => 1 [br] => 1 [input] => 1 [meta] => 1 [link] => 1 [hr] => 1 [base] => 1 [embed] => 1 [spacer] => 1 ) [block_tags:protected] => Array ( [root] => 1 [body] => 1 [form] => 1 [div] => 1 [span] => 1 [table] => 1 ) [optional_closing_tags:protected] => Array ( [tr] => Array ( [tr] => 1 [td] => 1 [th] => 1 ) [th] => Array ( [th] => 1 ) [td] => Array ( [td] => 1 ) [li] => Array ( [li] => 1 ) [dt] => Array ( [dt] => 1 [dd] => 1 ) [dd] => Array ( [dd] => 1 [dt] => 1 ) [dl] => Array ( [dd] => 1 [dt] => 1 ) [p] => Array ( [p] => 1 ) [nobr] => Array ( [nobr] => 1 ) ) [doc:protected] => [noise:protected] => Array ( ) )

【问题讨论】:

  • 你做过ANY基本调试吗,例如转储 $html 的值以确保您收到了一些东西?
  • 另外,为什么不使用内置的 DOMDocument 功能呢? ibm.com/developerworks/library/os-xmldomphp
  • 这是不精确的,但不会工作我的意思是不会输出任何东西。就像它可以识别功能和所有功能,但无法从其他站点抓取任何内容。

标签: php simple-html-dom dom


【解决方案1】:

您发布的代码对我来说很好用。

觉得你可以试试这些,把下面几行放在脚本上面:

    ini_set('error_reporting', E_ALL);
    ini_set('display_errors', 1);

还要检查 $html 的 print_r

    print_r($html);

希望对您有所帮助。

【讨论】:

  • 谢谢,前两个没有输出任何东西,看来脚本可以正常工作。现在我将更新 print_r() 方法的输出。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-09
  • 2017-04-26
  • 2017-12-11
相关资源
最近更新 更多