php抓取网页关键数值
想抓取目标网页日元对人民币汇率这个数值并生成JS,我改的代码输出只有0.0034。会的帮忙改下,谢谢。<?php
$arr = file("https://www.huilv.cc/JPY_CNY/");
if($arr){
foreach($arr as $a){
if(strchr($a,"<div class=dollar_two><span class=back>")){
$a = str_ireplace("<div class=dollar_two><span class=back>","",$a);
$a = str_ireplace("</span></div><div class=clear>","",$a);
$b = $a+0.0034;
echo $b ;
$content = " var rate = $b;
document.writeln('$b');";
$fp = fopen ('ka'. '.js',"w");
if (fwrite ($fp,$content)){
//格式是.int fwrite(int fp(文件名), string string(内容),
fclose ($fp);//函数用来关闭已经打开的文件的指针 fp。
die ("");
} else {
fclose ($fp);
die ("");
}
echo ($content);
break;
}
}
}
?> <?php
$arr = file("https://www.huilv.cc/JPY_CNY/");
if($arr){
preg_match("@<div class=dollar_two><span class=back>(.*?)</span></div><div class=clear></div><div class=@si",$arr, $regs);
$a = $regs;
$b = $a+0.0034;
echo $b ;
$content = " var rate = $b;
document.writeln('$b');";
file_put_contents("ka.js",$content);
}
?> 可以到威客上看看 本帖最后由 logo设计专家 于 2020-2-25 16:25 编辑
$a是个字符串强行加数字 就是0
要先把$a字符串转为数字
第9行改成
$b = number_format($a)+0.0034;
页:
[1]