phpQuery使用DOMDocument::loadHTML方法产生报错的处理方式

发现问题

在作爬虫的时候,用了QueryList,在运行的过程当中查看日志,出现不少关于phpQuery单文件的error报错,问题是出在html非标准化格式php

[ error ] [2]DOMDocument::loadHTML(): htmlParseEntityRef: expecting ';' in Entity, line: 1262[/home/querying/PhpstormProjects/xianlang10.com/EngineSeo/vendor/jaeger/phpquery-single/phpQuery.php:328]
[ error ] [2]DOMDocument::loadHTML(): htmlParseEntityRef: expecting ';' in Entity, line: 1262[/home/querying/PhpstormProjects/xianlang10.com/EngineSeo/vendor/jaeger/phpquery-single/phpQuery.php:328]

解决问题

在查看php手册关于DOMDocumentloadHTML方法的使用的时候,发现libxml_use_internal_errors能够对此类错误,强制以libxml_get_errors()进行获取,该函数返回内容一个迭代器html

<?php
	//所以咱们能够在调用loadHtml方法以前,先规避这个问题
	// enable user error handling
	libxml_use_internal_errors(true);

	// load the document
	$doc = new DOMDocument;

	if (!$doc->load('file.html')) {
		foreach (libxml_get_errors() as $error) {
			// handle errors here
		}
		libxml_clear_errors();
	}
?>
相关文章
相关标签/搜索