2012-09-26 34 views
0

獲得此稱號的html頁面:節點從一個HTML頁面

<div class="gs_ri"> 
    <h3 class="gs_rt"> 
    <span class="gs_ctc"> 
    <span class="gs_ct1">[BOOK]</span> 
    <span class="gs_ct2">[B]</span></span> 
    <a href="http://example.com" onmousedown="">Title</a></h3> 
<div class="gs_a">A</div> 
<div class="gs_rs">B</div> 
<div class="gs_fl"><a href="">C</a> <a href="">D</a> <a href=""</a></div></div></div> 
<div class="gs_r"><div class="gs_ggs gs_fl"><button type="button" id="gs_ggsB2" class="gs_btnFI gs_in_ib gs_btn_half"> 
    <span class="gs_wr"><span class="gs_bg"></span> 
    <span class="gs_lbl"></span> 
    <span class="gs_ico"></span></span></button> 
<div class="gs_md_wp" id="gs_ggsW2"><a href="http://example.pdf" onmousedown="" 

我有點困惑,以確定該節點。

我想獲得http://example.comTitle

我認爲有2種方式,讓他們:

它的<span>的兄弟:

foreach($html->find('span[class=gs_ctc2] ') as $link){ 
    $link = $link->next_sibling(); 
    echo $link->plaintext; 
    echo $link->href; 
} 

,但它不工作。

第二,我拿<h3 class="gs_rt">父,所以它的最後一個子

foreach($html->find('h3[class=gs_rt] a') as $link){ 
    $link = $link->last_child()->next_sibling(); 
    echo $link->plaintext; 
    echo $link->href; 
} 

的兄弟也不起作用。我認爲我並不瞭解節點樹。

回答

1

您不必選擇兄弟姐妹。

使用h3[class=gs_rt] a您已經針對相應的<a>標籤。所以只需從那裏提取所需的值。你可以,但是,如下簡化選擇:

foreach($html->find('h3.gs_rt a') as $link){ 
    echo $link->plaintext; 
    echo $link->href; 
} 

編輯

至於評論,我想,你想要的是這樣的事情,但我不知道,你的上面的代碼是相當混亂(請使用正確的縮進!)

foreach($html->find('h3.gs_rt') as $block){ 
    $link = $block->find('a'); 
    echo $link->plaintext; 
    echo $link->href; 

    $otherLink = $block->find('div[class=gs_md_wp] a'); 
    // do stuff with that $otherLink 
} 
+0

我明白了..謝謝! –

+0

對不起,我還有一個問題,先生。我在上面添加了html頁面。我可以通過'find('div [class = gs_md_wp]')獲得'http:// example.pdf'(在最後一行)$ docLink''$ link = $ docLink-> first_child();'If我想用你的代碼分組。如何確定節點?它應該是 - > –

+0

'foreach($ html-> find('h3.gs_rt a')as $ link){ foreach($ link-> parent() - > parent() - > parent() - > find ('div [class = gs_md_wp]')爲$ docLink)'?在此先感謝:) –

0

增加號碼的HREF

<a id="myid" href="http://example.com" onmousedown="javascript:get_title('#myid')">Title</a></h3> 

function get_title(i){ 
var h =$(i).attr('href'); 
var t =$(i).text(); 
alert('the link is (' + h + ') and the title is (' + t + ')'); 
     } 
+0

謝謝,但我更喜歡用簡單的html dom解析器,先生:) –