0

我在这个Statsoft 站点中有 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 ) {

但它给了我一个空白页。

我错过了什么?谢谢你。

4

1 回答 1

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();
于 2013-06-25T03:03:32.843 回答