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

发现问题

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

[ 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()进行获取,该函数返回内容一个迭代器

<?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();
	}
?>

转载于:https://my.oschina.net/querying/blog/1865153

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值