首页 > 编程技术 > php

php 模拟用户抓取网页内容程序

发布时间:2016-11-25 15:59

 代码如下 复制代码

function _link($url){
 $contents = @file_get_contents("$url");
 if($contents=="Forbidden" || $contents==""){
  $ch = curl_init();
  $timeout = 5;
  curl_setopt ($ch, CURLOPT_URL, "$url");
  curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
  curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  $contents = curl_exec($ch);
  curl_close($ch);
 }
 if(empty($contents)){
  exit('<font color=red>cant locaion.</font>');
 }
 preg_match_all("/charset=(.*?)>/is",$contents,$cod);
 if(!empty($cod[1][0])){
  if(preg_match("/utf-8/i",$cod[1][0])){
   $contents=iconv("UTF-8","gbk//TRANSLIT",$contents);
  }
 }
 return $contents;
}

<?php

function copyViaFtpRecursively($uploadLocation, $previewPath, $remoteDirectory, $ftpType)
        {
        $errorMessage = '';

        $connectionId = getFtpConnection($uploadLocation['host'], $uploadLocation['username'], $uploadLocation['password'], $uploadLocation['port']);
        switch($ftpType)
                {
                case 'active':
                        ftp_pasv($connectionId, False);
                        break;
                case 'passive':
                        ftp_pasv($connectionId, True);
                        break;
                }

        $baseDirectory = $uploadLocation['baseDirectory'];
        if(substr($baseDirectory, strlen($baseDirectory) - 1, 1) != '/')
                {
                $baseDirectory .= '/';
                }
        ftp_mkdir($connectionId, $baseDirectory); // No point showing an error message if the directory exists (most likely cause of error) because it will exist (at least) after the first time.

        $remoteBaseDirectory = $baseDirectory.$remoteDirectory;
        if(substr($remoteBaseDirectory, strlen($remoteBaseDirectory) - 1, 1) == '/')
                {
                $remoteBaseDirectory = substr($remoteBaseDirectory, 0, strlen($remoteBaseDirectory) - 1);
                }

        $remoteBaseDirectory .= '/';
        $errorMessage .= copyFileViaFtp($previewPath, $remoteBaseDirectory, $connectionId);

        ftp_close($connectionId);

        $errorHtml = '';
        if($errorMessage)
                {
                $errorHtml = nl2br($errorMessage);
                }
        return $errorHtml;
        }

function getFtpConnection($host, $username, $password, $port)
        {
        $connectionId = ftp_connect($host);
        if(!@ftp_login($connectionId, $username, $password))
                {
                webServiceError('FTP error. Unable to connect to "'.$host.'" with username "'.$username.'"');
                }
        return $connectionId;
        }


function copyFileViaFtp($sourcePath, $destinationPath, $connectionId)
        {
        $errorMessage = '';
        $sourcePath = str_replace(" ", "-", $sourcePath);
        $destinationPath = str_replace(" ", "-", $destinationPath);
        if(!ftp_mkdir($connectionId, $destinationPath))
                {
                $errorMessage .= "Unable to create directory at ".$destinationPath." (it may already exist) ";
                }
        ftp_site($connectionId, 'CHMOD 0777 '.$destinationPath);
        ftp_chdir($connectionId, $destinationPath);
        //print $sourcePath.' to '.$destinationPath."<br />";
        if(is_dir($sourcePath))
                {
                chdir($sourcePath);
                $handle=opendir('.');
                while(($file = readdir($handle))!==false)
                        {
                        if(($file != ".") && ($file != ".."))
                                {
                                if(is_dir($file))
                                        {
                                        $errorMessage .= copyFileViaFtp($sourcePath.DIRECTORY_SEPARATOR.$file, $file, $connectionId);
                                        chdir($sourcePath);
                                        if(!ftp_cdup($connectionId))
                                                {
                                                $errorMessage .= "Unable to ftp_cdup. ";
                                                }
                                        }
                                else
                                        {
                                        if(substr($file, strlen($file) - 4, 4) != ".zip")
                                                {
                                                $fp = fopen($file,"r");
                                                if(!ftp_fput($connectionId, str_replace(" ", "_", $file), $fp, FTP_BINARY))
                                                        {
                                                        $errorMessage .= "Unable to ftp_fput(). ";
                                                        }
                                                ftp_site($connectionId, 'CHMOD 0755 '.str_replace(" ", "_", $file));
                                                }
                                        }
                                }
                        }
                closedir($handle);
                }

        return $errorMessage;
        }

 代码如下 复制代码

preg_match_all("/<a(.*?)href=(.*?)</a>/i",$webContent,$link);
 $urls =array();
 foreach($link[0] as $value)
 {
  if(strstr($value,'http') )
  {
   if(strstr($value, $ex[3]) || strstr( $value,$ex[0]) || strstr($value,$ex[1]) || strstr($value,$ex[2]))
   {
     continue;
   }
   else
   {
    preg_match_all("/hrefs*=s*(['"]?)(.*?)\1/is", $value, $vlink);    
    $urls[] = $vlink[2];
   }
  }
 }
 $strUrl = deleteEmptyArray( $urls);

function deleteEmptyArray( $val )
{
 $links ='';
 if( is_array( $val ) )
 {
  foreach( $val as $v =>$_v)
  {
   if( !empty( $_v[0] ) )
   {
    $links .=$_v[0].'|';
   }
  }  
  return substr($links,0,-1);
 }
 else
 {
  return false;
 }
}

 

1. 所需软件环境:
1. apache 2.2
2. active perl 5.8 c:perl
3. php 5.1 必须安在 c:php
4. ZendOptimizer-3.0.2
5. php环境所需 includes --在 php 安装之后,无法正常运行时必须的库。
-----------------------------------------------
2. apache 2.2--------httpd.conf 文件配置
1. 第133行前面加 #
2. 修改 <IfModule alias_module> </IfModule> 之间
ScriptAlias /cgi-bin/ "D:/Programmes/Server/Apache2.2/cgi-bin/"

<Directory "D:/Programmes/Server/Apache2.2/cgi-bin">
划线部分分别改为 C:/php/ 和 C:/php
3. 将 #!c:/program files/perl/perl 改为 #!C:/Perl/bin
4. 在文件末尾添加
ScriptAlias /php/ "c:/php/"
AddType application/x-httpd-php .php

# For PHP 4
#Action application/x-httpd-php "/php/php.exe"
# For PHP 5
Action application/x-httpd-php "/php/php-cgi.exe"
-------------------------------------------------------------------------------
3. php配置细则:
1. 将 c:php 加入 windows 环境变量 path 中
2. 将 includes 拷入 c:phpincludes
3. 将 php.ini-dist 改名为 php.ini
4. 下面是 php.ini 配置细则:
1. 将 doc_root = 设置为 doc_root = D:ProgrammesServerApache2.2htdocs
2. 将 extension_dir = 设置为 extension_dir = "C:phpext"
3. 将 ;extension=php_mysql.dll 前面的 ”;” 去掉,改为 extension=php_mysql.dll
4. 在 ; Windows: "path1;path2" 下面 的 include_path = 改为
include_path = ".;c:phpincludes"
5. 对 [MySQL] 下面进行配置
4. active perl 安装很简单照着指示做就可以 最好安到 c:perl
5. ZendOptimizer 安装时把 apache 服务器关掉,在过程中要指定 apache 和 php 的安装路径。

<? phpinfo() ?>

setlocale() mktime()
getdate 获得时间及日期信息。
语法: array getdate(int timestamp);
返回值: 数组
函数种类: 时间日期
内容说明 返回数组的元素包括下列的项目:
"seconds" - 秒
"minutes" - 分
"hours" - 时
"mday" - 当月的第几天
"wday" - 当周的第几天数字
"mon" - 月份数字
"year" - 年,数字
"yday" - 当年的第几天数字; 如: "299"
"weekday" - 星期几全名; 如: "Friday"
"month" - 月份全名; 如: "January"
gettimeofday 取得目前时间。
语法: array gettimeofday(void);
返回值: 数组
函数种类: 时间日期
内容说明 返回数组的元素包括下列的项目:
"sec" - 秒
"usec" - 百万分之一秒
"minuteswest" - 格林威治时间的分
"dsttime" - 目的的时区
gmdate 取得目前与 GMT 差后的时间。
语法: string gmdate(string format, int timestamp);
返回值: 字符串
函数种类: 时间日期
内容说明 本函数与 date() 函数相似,不同的是本函数返回与格林威治标准时间 (Greenwich Mean Time, GMT) 时间差后的时间
使用范例
<?php
echo date( "M d Y H:i:s",mktime(0,0,0,1,1,1998) );
echo gmdate( "M d Y H:i:s",mktime(0,0,0,1,1,1998) );
?>
若执行本范例的机器在芬兰 (Finland, GMT +0200),返回的结果为:
Jan 01 1998 00:00:00
Dec 31 1997 22:00:00
参考 date() mktime() gmmktime()
easter_date 计算复活节日期。
语法: int easter_date(int [year]);
返回值: 整数
函数种类: 时间日期
内容说明: 输入某一年份,则以 UNIX 时间戳记的格式返回该年的复活节日期,若没有输入年份,则计算当年的日期。值的?注意的是输入的年份必需在公元 1970 至 2037 年之间,否则无法计算。
使用范例
<?
echo date("M-d-Y", easter_date(1999));
echo date("M-d-Y", easter_date(2000));
echo date("M-d-Y", easter_date(2001));
?>
返回结果为
Apr-04-1999
Apr-23-2000
Apr-15-2001
easter_days 计算复活节与三月廿一日之间日期数。
语法: int easter_days(int [year]);
返回值: 整数
函数种类: 时间日期
内容说明 输入某一年份,则以计算该年复活节与三月二十一日之间的日期数,若没有输入年份,则以当年计算。这个函数可以用来替代 easter_date() 1970-2037 年外范围无法计算的问题。
使用范例
<?
echo easter_days(1999);
echo easter_days(1492);
echo easter_days(1913);
?>
返回结果为:
14 (4/4)
32 (4/22)
2 (3/23)
参考 easter_date()
mktime 取得 UNIX 时间戳记。
语法: int mktime(int hour, int minute, int second, int month, int day, int year);
返回值: 整数
函数种类: 时间日期
内容说明: 输入一个时间,则返回 UNIX 时间戳记的长整数。
使用范例
<?
echo date( "M-d-Y", mktime(0,0,0,12,32,1997) );
echo date( "M-d-Y", mktime(0,0,0,13,1,1997) );
echo date( "M-d-Y", mktime(0,0,0,1,1,1998) );
?>
参考 date() time()
gmmktime 取得 UNIX 时间戳记的格林威治时间。
语法: int gmmktime(int hour, int minute, int second, int month, int day, int year);
返回值: 整数
函数种类: 时间日期
内容说明: 输入一个时间,则返回 UNIX 的格林威治时间戳记的长整数。
time 取得目前时间的 UNIX 时间戳记。
语法: int time(void);
返回值: 整数
函数种类: 时间日期
内容说明 返回目前时间的戳记值。
参考 date()
microtime 取得目前时间的 UNIX 时间戳记的百万分之一秒值。
语法: string microtime(void);
返回值: 字符串
函数种类: 时间日期
内容说明 返回目前时间的百万分之一秒戳记值。若操作系统没有提供 gettimeofday() 的系统呼叫函数,则本函数亦无效。

标签:[!--infotagslink--]

您可能感兴趣的文章: