简体   繁体   中英

php simple html dom parse a tag except in some div

some test get a tag from http://www.msnbc.msn.com/ use simple html dom.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.msnbc.msn.com/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5');
$htmls = curl_exec($ch);
curl_close($ch);
$html = str_get_html($htmls);
foreach($html->find('a') as $element){ 
    echo $element.'<br />';
}

this code could get all the hyper links, but how to ignore all the links in div#mainNav ? I need get all the links out of div#mainNav in http://www.msnbc.msn.com/ , thanks.

Check the parent, like this:

foreach($html->find('a') as $element){ 
    if ($element->parent()->id == 'mainNav') {
        //do nothing
    } else {
        echo $element.'<br />';
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM