2013-06-25 56 views
0

我在這Statsoft site HTML文件中,特別是在這一部分:從具有其他孩子標籤中獲取文本

<p> 
    <a name="Z Distribution (Standard Normal)"> 
     <font color="#000080" size="4"> 
      Z Distribution (Standard Normal). 
     </font> 
    </a> 
    The Z distribution (or standard normal distribution) function is determined by the following formula: 
</p> 

我想要的文字The Z distribution (or standard normal distribution) function is determined by the following formula:和我寫了一些代碼是這樣的:

include('simple_html_dom2.php'); 
$url = 'http://www.statsoft.com//textbook/statistics-glossary/z/?button=0#Z Distribution (Standard Normal)'; 
$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$curl_scraped_page = curl_exec($ch); 
$html = new simple_html_dom(); 
$html->load($curl_scraped_page); 

foreach ($html->find('/p/a [size="4"]') as $e) { 
    echo $e->innertext . '<br>'; 
} 

它只是給了我:Z Distribution (Standard Normal).

我試着寫

foreach ($html->find('/p/a [size="4"]/font') as $e) { 

,但它給了我一個空白頁。

我錯過了什麼?謝謝。

回答

0

找到段落,然後從鏈接的文本:

include('simple_html_dom2.php'); 
$url = 'http://www.statsoft.com//textbook/statistics-glossary/z/?button=0#Z Distribution (Standard Normal)'; 
$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$curl_scraped_page = curl_exec($ch); 

$html = new simple_html_dom(); 
$html->load($curl_scraped_page); 

foreach ($html->find('/p/a [size="4"]') as $font) { 
    $link = $font->parent(); 
    $paragraph = $link->parent(); 

    $text = str_replace($link->plaintext, '', $paragraph->plaintext); 

    echo $text; 
} 

原來的答覆:

你的問題與此相關的一個:Getting the text between two spans with "Simple HTML DOM"

你的選擇是找到在font標籤,它的父(a標籤)是你想要的文字的兄弟:

$text = $html->find('/p/a', 0)->next_sibling(); 
+0

它仍然無法正常工作,我該怎麼辦? – Fii

+0

查找'/ p',即元素中,找到了''和刪除從'了' –

+0

文字請你把這些代碼到我的代碼?我想我不知道如何把你的解決方案放到我的問題中。謝謝。 – Fii