繁体   English   中英

simplehtmldom获得标题的href

[英]simplehtmldom get href of a title

我下面的代码可以正常工作,但是缺少一件事。 它不会返回$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] => 
        )

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

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

即使simple_html_dom是在jQuery之后建模的,它的API也不能完全映射到jQuery。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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