php抓取alexa网页内容 提取站点统计信息
任务:根据输入的域名 统计以下三个数据,第一:全球排名;第二:用户量(月平均值);第三:
人均页面访问量(月平均值)。
思路:使用get_file_contents提取出网页内容,再根据正则表达式进行内容的筛选。
核心函数如下:
<?php
/*
the function of getting aleax data
@param string partten ;the url set
@return array partten ;
*/
function get_alexa_data($url)
{
$a_url = explode(PHP_EOL,$url);
$index = count($a_url);
if(function_exists('file_get_contents'))
{
$result = array();
$pattern = "|<div\\sclass=\"data[^\"]+?\">[^>]*?>[^>]*?([\\d,]+)\\s*?</div>[\\S\\s]*?id=\"reach[\\S\\s]*?<th>1".
"\\smonth[\\S\\s]*?class=\"avg.*?([\\d\\.]+)</td[\\S\\s]*?id=\"pageviews_per_user\"[\\S\\s]*?".
"1\\smonth[\\S\\s]*?class=\"avg.*?([\\d\\.]+)</td|";
for($i = 0; $i < $index; $i++)
{
if(!empty($a_url[$i]))
{
$content = file_get_contents('http://www.alexa.com/siteinfo/'.$a_url[$i]);
if(!preg_match($pattern,$content,$result[$a_url[$i]]))
$result[$a_url[$i]]=array('Not Found','Not Found','Not Found','Not Found');
}
}
}
else
die("the function file_get_contents not found!");
return $result;
}
?>
改变上面的正则表达式,可以满足自己的另外要求。
上面函数使用的前置条件是:alexa提供的查询网址格式'http://www.alexa.com/siteinfo/‘不变。
使用例程:
<table>
<tr>
<td>site</td><td>全球排名</td><td>用户量(月平均值)</td><td>人均页面访问量(月平均值)</td>
</tr>
<?php
$alexa = get_alexa_data($_POST['url']);
foreach($al
相关文档:
一:结构和调用(实例化):
class className{} ,调用:$obj = new className();当类有构造函数时,还应传入参数。如$obj = new className($v,$v2...);
二:构造函数和析构函数:
1、构造函数用于初始化:使用__construct(),可带参数。
2、但析构函数不能带参数(用于在销去一个类之前执行一些操作或功能)。析构函数用 ......
查找mysql_pconnect时在mysql帮助文档上发现的,记在这里。
web server使用php生成一个web页面的三式有以下三种:
一:把PHP作为CGI Wrapper。这种方式下,每个到达web server的请求都会导致一个php解析器进程被创建,当这个php页面执行结束时,这个php解析器进程终止。
二:
在多进程的web server中,把php作为web
se ......
准备:
lighttpd-1.4.15.tar.gz
php-4.4.2.tar.gz
mysql-5.0.20a.tar.gz
开始:
1 编译安装lighttpd
# tar zxvf lighttpd-1.4.15.tar.gz
# cd lighttpd-1.4.15
# ls
# ./configure --prefix=/usr/local/lighttpd //此部无法编译时提示安装prce-devel
#&nbs ......
<?php
function delfile($dir,$n) //删除DIR路径下N天前创建的所有文件;
{
if(is_dir($dir))
{
if($dh=opendir($dir))
{
while (false !== ($file = readdir($dh)))
{
if($file!="." && $file!="..")
{
$fullpath=$dir."/".$file;
if(!is_dir($fullpath)) ......