0

我正在使用simple_html_dom并且我正在使用find()方法来查找页面上的特定元素。但是该元素 ( div) 是可选的。有些页面有,有些页面没有。因此,当find()方法尝试查找不存在的 div 时,我的所有脚本都崩溃了。在那条线上它停止了。我该如何处理find()

我像使用它一样

....
....
$html = file_get_html($requestUrl);
$result = $html->find("div[id=searchCategoryContainer] ul li")); //problem is here
.... I cant reach these lines if above div is not existed....
....
....

我也尝试使用 try - catch 块,但它仍然不起作用。

try{
    $html = file_get_html($requestUrl);
    $result = $html->find("div[id=searchCategoryContainer] ul li"));
    throw new Exception("there is no element like this!!!");

}catch(Exception $e){
    echo $e->getMessage();
}
4

0 回答 0