使用PHP进行DomDocument和HTML解析

时间:2013-07-09 22:39:40

标签: php domdocument

我正在写这个答案,因为我是DomDocuement的新手,我无法在其他任何地方找到答案。

我正在为CMS网站编写自定义WordPress主题。一个要求是从其他站点检索网页内容,选择要显示的内容的一部分,然后参考原始站点以获取完整内容。

因此我使用WordPress中的wp_remote_get函数来获取网页。然后我必须解析它去特定的网页元素,我使用Domdocument来解析页面如下

$dom = new domDocument();
$dom->loadHTML($html); // Where $html is the page retrieved earlier using wp_remote_get

现在,问题是loadHTML()导致了这些警告:

PHP Warning:  DOMDocument::loadHTML(): DOCTYPE improperly terminated in Entity, Lline: 2
PHP Warning:  DOMDocument::loadHTML(): htmlParseStartTag: misplaced <html> tag in Entity, line: 3
PHP Warning:  DOMDocument::loadHTML(): htmlParseStartTag: misplaced <head> tag in Entity, line: 4
PHP Warning:  DOMDocument::loadHTML(): htmlParseStartTag: misplaced <body> tag in Entity, line: 105
PHP Warning:  DOMDocument::loadHTML(): ID 1 already defined in Entity, line: 551

还有更多警告。

现在显然这个网页出了问题,但是我们需要检索的大多数网页会产生这些错误。

我的问题是:

  1. 我应该担心这些警告
  2. 有没有办法告诉domdocuemnt忽略那些不完美之处?
  3. 如何才能使事情正常运作
  4. 感谢您的输入

0 个答案:

没有答案