2013-07-05 165 views
1

我正在使用PHP简单的HTML DOM解析器从10.000+页获取电子邮件地址。简单的HTML DOM解析器错误

require_once('simple_html_dom.php'); 
$html = file_get_html('http://www.myurl'); 
$email = $html->find('dl', 5)->children(3); 

有时会得到跟随误差。可能是因为某些网页的没有标签:

​​

如何避免这种错误,如果一个页面中不包含我正在寻找的信息,而无需中断完整的脚本?

+0

'find()'找不到任何东西,所以它返回的没有 - > children()方法。 –

回答

2

你可以测试,看看是否$email是使用is_object()功能,例如一个对象

$email = $html->find('dl', 5); 
if(is_object($email) === true) 
{ 
    print_r($email->children(3)); 
} 
else continue; 

这可能比测试使用外部库的孩子更快,因为它使用PHP引擎中已经存在的函数。

2

如果存在不确定性,请不要链接您的方法。分配$email = $html->find('dl', 5) - 再测试,如果有任何的孩子,可能与hasChildNodes