简体   繁体   中英

simplehtmldom get href of a title

I have a code below which is working correctly but is missing one thing. It is not returning the value for the $item['link'] which is the href of the title.

include('simple_html_dom.php');
$html = file_get_html('http://news.google.com/news/section?pz=1&cf=all&ned=us&q=newzealand'); 


foreach($html->find('.blended-wrapper') as $article) {
    $item['title']     = $article->find('span.titletext', 0)->plaintext;
    $item['source']    = $article->find('span.esc-lead-article-source', 0)->plaintext;
    $item['clip'] = $article->find('div.esc-lead-snippet-wrapper', 0)->plaintext;

    $item['link'] = $article->find('.esc-lead-article-title a')->href;

    $articles[] = $item;
}

echo "<pre>";
print_r($articles);
echo "<pre/>";

This is what it outputs, as you see the link key is empty. I've tried so many things. Array

(
    [0] => Array
        (
            [title] => New Zealand dominate 2011 after 24 years of pain
            [source] => Times of India
            [clip] => WELLINGTON: After 24 years of stumbles, disappointments and plain old chokes, New Zealand finally lived up to their billing as world rugby's premier side in 2011.
            [link] => 
        )

    [1] => Array
        (
            [title] => PRESS DIGEST-New Zealand newspapers - Dec 29
            [source] => Reuters
            [clip] => WELLINGTON Dec 29 (Reuters) - Following are some of the lead stories from New Zealand metropolitan newspapers on Thursday. Stories may be taken from either the paper or Internet editions of the papers.
            [link] => 
        )

You have to get the first one of the links, even though there's only one in the collection:

$item['link'] = $article->find('.esc-lead-article-title a', 0)->href;

Even though simple_html_dom is modeled after jQuery, it's API does not map exactly to jQuery.

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