simplehtmldom获取标题的href

时间:2011-12-28 21:59:42

标签: php arrays simple-html-dom

我在下面有一个代码正常工作,但缺少一件事。它没有返回$item['link']的值,即标题的href

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/>";

这是它输出的内容,因为您看到链接键为空。我尝试了很多东西。 阵列

(
    [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] => 
        )

1 个答案:

答案 0 :(得分:1)

你必须得到第一个链接,即使集合中只有一个:

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

尽管simple_html_dom是在jQuery之后建模的,但它的API并不完全映射到jQuery。