1

我正在尝试从外部其他域的跨度、类、div 中获取内容到我的个人网站。网站 www.example.com 的源代码是

    <div id="h_gold" class="h_metal">
      <div class="hm_inside">
        <div class="hm_title">Gold</div>
        <span class="arr_price_down">- 13.42 <img src="images/downarrow.jpg" alt="down" /></span>
        <div class="clear"></div>
        <div class="hm_cad">USD</div>
        <div class="hm_bottom">
          <div> Bid<br />
            <span>$1,749.20</span> </div>
          <div class="ask"> Ask<br />
            <span>$1,750.20</span> </div>
        </div>
        <!-- .hm_bottom-->
      </div>
      <!-- hm_inside-->
    </div>
    <!-- h_metal-->
    <div id="h_silver" class="h_metal">
      <div class="hm_inside">
        <div class="hm_title">Silver</div>
        <span class="arr_price_down">- 0.54 <img src="images/downarrow.jpg" alt="down" /></span>
        <div class="clear"></div>
        <div class="hm_cad">USD</div>
        <div class="hm_bottom">
          <div> Bid<br />
            <span>$33.40</span> </div>
          <div class="ask"> Ask<br />
            <span>$33.50</span> </div>
        </div>
        <!-- .hm_bottom-->
      </div>
      <!-- hm_inside-->
    </div>
    <!-- h_metal-->

我这里有问题..我尝试了以下代码..但它没有循环,我只从第一个 < div class="hm_bottom" > 获取内容,其余部分不在输出中,

    <?php
    function get_string_between($string, $start, $end)
    {
        $string = " ".$string;
        $ini = strpos($string,$start);
        if ($ini == 0)
            return "";
        $ini += strlen($start);
        $len = strpos($string,$end,$ini) - $ini;
        return substr($string,$ini,$len);
    }
    $data = file_get_contents("http://www.example.com/ajax.php?metal_cur=USD");

    $pricediv = get_string_between($data, '<div class="hm_bottom">', '</span>');
    $pricetext = strip_tags($pricediv);
    echo $pricetext;
    ?>

其次,我再次搜索了 stachoverflow,我发现一个已解决,但并不完全符合我的需求。输出还可以,但我只想将输出中的数字数据放在我定义的特定 div id 中。

    <?php 
    $page = file_get_contents('http://www.example.com/ajax.php?metal_cur=USD');
    $doc = new DOMDocument();
    $doc->loadHTML($page);
    $divs = $doc->getElementsByTagName('div');
    foreach($divs as $div) {
        // Loop through the DIVs looking for one withan id of "content"
        // Then echo out its contents (pardon the pun)
        if ($div->getAttribute('class') === 'hm_bottom') {
             echo $div->nodeValue;
        }
    }
    ?>

上面的代码来自stackoverflow的第二次搜索

4

1 回答 1

3

您可以根据情况使用str_get_htmlfile_get_html

$html = str_get_html($html);
$amounts = array();
foreach ( $html->find("div[class=hm_bottom] span") as $span ) {
    $amount = trim($span->plaintext);
    if (strpos($amount, "\$") === 0)
        $amounts[] = $amount;
}

var_dump($amounts);

输出

array
  0 => string '$1,749.20' (length=9)
  1 => string '$1,750.20' (length=9)
  2 => string '$33.40' (length=6)
  3 => string '$33.50' (length=6)
于 2012-10-13T17:28:00.767 回答