我正在尝试将span,class,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-->
我这里有一个问题..我尝试了以下代码..但它没有循环,我只从第一个&lt; div class =“hm_bottom”&gt; ,休息不在输出中,
<?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的第二次搜索
答案 0 :(得分:4)
您可以根据具体情况使用str_get_html或file_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)