PHP Query DOMDocument::loadHTML(): Unexpected end tag : span in Entity

968 阅读1分钟

我在MAMP上首先运行代码,工作得很好。但是当我尝试在其他服务器上运行代码时,我收到了很多警告,如:

[warning] DOMDocument::loadHTML(): Unexpected end tag : span in Entity, line: 168 /vendor/jaeger/phpquery-single/phpQuery.php:328
[warning] DOMDocument::loadHTML(): Unexpected end tag : td in Entity,
[warning] DOMDocument::loadHTML(): Unexpected end tag : b in Entity, line: 233 /vendor/jaeger/phpquery-single/phpQuery.php:328
[warning] DOMDocument::loadHTML(): error parsing attribute name in Entity, line: 523 /vendor/jaeger/phpquery-single/phpQuery.php:328

代码如下:

<?php
 $amazon = file_get_contents('http://www.amazon.com/blablabla');
 $doc = new DOMdocument();
 $doc->loadHTML($amazon);
 $doc->saveHTML();
 $price = $doc -> getElementById('actualPriceValue')->textContent;
 $ASIN = $doc -> getElementById('ASIN')->getAttribute('value');
?>

要禁用警告,可以使用

libxml_use_internal_errors(true);

背景:正在加载无效的HTML。无效的HTML是很常见的,DOMDocument::loadHTML纠正了大多数问题,但默认情况下会发出警告。

使用libxml_use_internal_errors可以控制这种行为。在加载文档之前设置它:

libxml_use_internal_errors(true);

$doc->loadHTML($amazon);