youkangta 2006-7-13 01:34 PM
提取网页数据的php代码
这段代码是论坛首页显示最新油价的php代码,这类代码把一个指定的源网页视作字符串,并通过字符串中的规则将有用的信息提取出来。大家可以参考一下,修改修改还可以用来提取其他local信息来丰富论坛功能
updategas.php
[code]
<html>
<?php
//将待提取的网页转换为字符串
$url="http://www.automotive.com/gas-prices/33/massachusetts/hampshire/amherst/index.html";
$str=@file_get_contents($url);
//切割字符串提取数据
//get update time
$tmp = explode("Last Updated:", $str);
$updatetime = substr($tmp[1], 0, 114);
$updatetime = strip_tags($updatetime);
echo $updatetime;
$len = strlen($str);
$partstr = strstr($str, '<table cellspacing="0" cellpadding="2" rules="all"');
$strarray = explode("Automotive.com is not affiliated", $partstr);
$original_table = $strarray[0]; //源网页的表格部分
$strarray = explode('<td class="Copy_Sub">', $original_table);
//获取表格中的数据
for($i=1;$i<count($strarray);$i++) {
$arr = explode('</td><td class="Copy_Sub" align="Center">',$strarray[$i]);
//$arr[0]: name and address
$arr[0] = str_replace('<font class="Copy_Primary_Sub_Bold">', "", $arr[0]);
$arr[0] = str_replace('</font>', "@", $arr[0]);
$arr[0] = str_replace('<br>', "", $arr[0]);
$newarr = explode("@", $arr[0]);
$name[$i-1]=$newarr[0];
$name[$i-1] = trim($name[$i-1]);
$addr[$i-1]=$newarr[1];
$addr[$i-1] = trim($addr[$i-1]);
//$arr[1]: regular price
$start = strpos($arr[1], "$");
if($start>=0) {
$reg_price[$i-1] = substr($arr[1], $start+1, 6);
if ($reg_price[$i-1]=="/A") {
$reg_price[$i-1]=0;
}
$reg_price[$i-1]=(float)$reg_price[$i-1];
$reg_price[$i-1] = trim($reg_price[$i-1]);
}
}
//将提取出的数据写入文件
$filename = './cache/data.txt';
//put everything in a single string
$tmpstr[0] = implode(":", $name);
$tmpstr[1] = implode(":", $addr);
$tmpstr[2] = implode(":", $reg_price);
$tmpstr[3] = $updatetime;
$savestr = implode("$$", $tmpstr);
// 首先确定文件存在并且可写。
if (is_writable($filename)) {
// 使用添加模式打开$filename,
if (!$handle = fopen($filename, 'w+')) {
print "不能打开文件 $filename";
exit;
}
// 将$savestr写入到打开的文件中。
if (!fwrite($handle, $savestr)) {
print "不能写入到文件 $filename";
exit;
}
print "成功地将 $savestr 写入到文件$filename";
fclose($handle);
} else {
print "文件 $filename 不可写";
}
?>
</html>
[/code]
下面的代码用来读出存储的数据,并用柱状图显示
[code]
<?php
class histogram_h
{
/***********************************
* H: horizontal
* HTML 柱状图
* 使用方法:
* $htg = new histogram_h;
* $htg->num = "250,370,621,720,326,455,132,345,611,126,551,266";
* $htg->label = "1月,2月,3月,4月,5月,6月,7月,8月,9月,10月,11月,12月";
* $htg->show();
************************************/
//数值
var $num;
//低部标签值
var $label;
var $label2;
//柱状图宽度, 默认为399, 用户可自定义
var $width=130;
//每条柱的高度,默认为25, 用户可自定义
var $perheight=8;
//柱与柱之间的间隔
var $cellspacing=0;
function show()
{
//获取外部数值
$shu=$this->num;//split(",",$this->num);
//获取标签值
$biaoqian=$this->label;//split(",",$this->label);
//颜色, 默认为20种, 如果客户数据超过20种, 则回到第一种, 如此循环.
$color = array('#FF9900','#333399','#003300',
'#009900','#990099','#003300',
'#996600','#3300CC','#CC0000',
'#669900','#FF3300','#9900FF',
'#557e27','#009337', '#97bd00','#009900','#cc3300',
'#ffcc00','#3366cc','#33cc33');
if ($shu=="") die("error id:1");
$shuju=split(",",$shu);
//计算最大值
for($i=0;$i<count($shu);$i++)
{
if(!is_numeric($shu[$i])) die("error id:2");
if($shu[$i]>$zuidashu) $zuidashu=$shu[$i];
}
//计算最小值
$minimum = 10;
for($i=0;$i<count($shu);$i++)
{
if(!is_numeric($shu[$i])) die("error id:2");
if(($shu[$i]<$minimum) && ($shu[$i]>0)) $minimum=$shu[$i];
}
?>
<table border="1" cellpadding="0" cellspacing=" <?php echo $this->cellspacing; ?> ">
<?php
for($i=0,$j=0;$i<count($shu);$i++,$j++)
{
if($j>19){$j=0;}
?>
<tr>
<td>
<table border="0" cellpadding="0" cellspacing="0" width="130" height="10">
<tr> <td><p align="left"> <font size=1 face="Arial" color="<?php /*颜色*/ echo $color[$j]; ?>"> <B><?php echo /*打印左边标签*/ $biaoqian[$i]; ?></B> </font></td>
<tr> <td><p align="left"> <font size=1 face="Arial"> <?php echo $this->label2[$i]; ?> </font> </td>
</table>
</td>
<td>
<table border="0" cellpadding="0" cellspacing="0" height=" <?php echo $this->perheight; ?> ">
<tr>
<td height="15" width=" <?
echo floor((($this->width-50)/($zuidashu-$minimum))*($shu[$i]-$minimum))+50;?>" bgcolor="<?php /*柱状颜色*/ echo $color[$j]; ?>"> </td>
</tr>
<tr> <td> <font size=1 face="Arial"> <?php /*在柱状图的顶端输出数值*/ echo $shu[$i]; ?> </font> </td> </tr>
</table>
</td>
</tr>
<?php
}
?>
</table>
<?php
} //End function
} // End histogram Class
?>
<body>
<?php
//read cache file
$datastring = file_get_contents ("./cache/data.txt");
// echo $datastring
$tmpstr = explode("$$", $datastring);
$name = explode(":", $tmpstr[0]);
$addr = explode(":", $tmpstr[1]);
$reg_price = explode(":", $tmpstr[2]);
$updatetime = $tmpstr[3];
?>
<font size=1 face="Arial" color="#333399"> <B><?php echo "Update time: "; echo $updatetime; ?></B> </font>
<?
//draw table
$htg = new histogram_h;
for($k=0;$k<count($name);$k++) {
$htg->label[$k]=$name[$k];
$htg->label2[$k]=$addr[$k];
$htg->num[$k]=$reg_price[$k];
}
$htg->show();
?>
</body>
[/code]
chao 2006-7-13 02:11 PM
大家可以对比着网站的html代码看看youkangta是怎么把有用信息在网页里面提取出来的
网址是:
[url]http://www.automotive.com/gas-prices/33/massachusetts/hampshire/amherst/index.html[/url]
Sherry 2006-7-13 02:23 PM
看到很多$
imac 2006-7-14 01:30 AM
老大... 这个回复好醒目... -_-b
不过估计早年在UNIX系列操作系统下开发的牛人们,也是穷极思变...
没有银子,每天看着多一些$也好...
[quote]原帖由 [i]Sherry[/i] 于 2006-7-13 01:23 PM 发表
看到很多$ [/quote]