0

我正在尝试通过将简单的 URL 作为 DOM 对象加载并使用 xPath 查询 DOM 来学习如何使用 xPath 和 PHP。我遇到了很多麻烦,并且一直在尝试对这些警告消息进行故障排除很长一段时间,但没有成功。

我感谢任何关于为什么会发生这种情况的建议。

提前谢谢了!

PHP

<?php
$doc = new DOMDocument();
$doc->loadHTMLFile("http://n5a.software.com/calendar/da2013/profile.php?__id=691");

$xpath = new DOMXpath($doc);

$elements = $xpath->query("//ul[@class='flper']/li[2]");

if (!is_null($elements)) {
    foreach ($elements as $element) {
        $nodes = $element->childNodes;
        foreach ($nodes as $node) {
            echo $node->nodeValue. "\n";
        }
    }
}

?>

输出

Warning: DOMDocument::loadHTMLFile(): Attribute style redefined in http://n5a.software.com/calendar/da2013/profile.php?__id=691, line: 92 in C:\xampp\htdocs\www\test\xpath.php on line 3

Warning: DOMDocument::loadHTMLFile(): htmlParseEntityRef: expecting ';' in http://n5a.software.com/calendar/da2013/profile.php?__id=691, line: 101 in C:\xampp\htdocs\www\test\xpath.php on line 3

Warning: DOMDocument::loadHTMLFile(): Attribute cellspacing redefined in http://n5a.software.com/calendar/da2013/profile.php?__id=691, line: 109 in C:\xampp\htdocs\www\test\xpath.php on line 3

Warning: DOMDocument::loadHTMLFile(): Unexpected end tag : span in http://n5a.software.com/calendar/da2013/profile.php?__id=691, line: 165 in C:\xampp\htdocs\www\test\xpath.php on line 3

Warning: DOMDocument::loadHTMLFile(): Unexpected end tag : script in http://n5a.software.com/calendar/da2013/profile.php?__id=691, line: 166 in C:\xampp\htdocs\www\test\xpath.php on line 3

Warning: DOMDocument::loadHTMLFile(): htmlParseEntityRef: no name in http://n5a.software.com/calendar/da2013/profile.php?__id=691, line: 197 in C:\xampp\htdocs\www\test\xpath.php on line 3

Warning: DOMDocument::loadHTMLFile(): Unexpected end tag : td in http://n5a.software.com/calendar/da2013/profile.php?__id=691, line: 205 in C:\xampp\htdocs\www\test\xpath.php on line 3

Warning: DOMDocument::loadHTMLFile(): Unexpected end tag : tr in http://n5a.software.com/calendar/da2013/profile.php?__id=691, line: 230 in C:\xampp\htdocs\www\test\xpath.php on line 3

Warning: DOMDocument::loadHTMLFile(): Unexpected end tag : table in http://n5a.software.com/calendar/da2013/profile.php?__id=691, line: 231 in C:\xampp\htdocs\www\test\xpath.php on line 3
4

1 回答 1

4

使用http://www.php.net/manual/en/function.libxml-use-internal-errors.php禁用警告libxml_use_internal_errors(true)

它是格式错误的 HTML,如果您不控制 HTML,您将无能为力。

于 2013-06-29T02:28:14.707 回答