抓取网页内容类

小静小哥_7745

小静小哥_7745

2016-06-21

934人浏览

原创

网页

 class thief{
  // 需要得到数据的网址
  var $url;
  // 需要分析的开始标记
  var $startflag;
  //需要分析的结束标记
  var $endflag;
  //存储图片的路径
  var $saveimagepath;
  //访问图片的路径
  var $imageurl;
  // 列表内容
  var $listcontent;
  //需要获得的图片路径
  var $imagelist;
  //存储的图片名称
  var $filename;
 
  /**
  * 得到页面内容
  * @return string 列表页面内容
  */
 
  function getpagecontent ()
  {               
   $pagecontent = @file_get_contents( $this->url );
  
   return $pagecontent;
  }
 
 
  /**
  * 根据标记得到列表段
  * @param $content  页面源数据
  * @return string   列表段内容
  */
 
  function getcontentpiece ( $content )
  {
   $content = $this->getcontent( $content, $this->startflag, $this->endflag );
                                                     if(!$content)  $content=$this->cut ($content, $this->startflag, $this->endflag );
   return $content;
  }
 
 
  /**
  * 得到一个字符串中的某一部分
  * @param $sourcestr 源数据
  * @param $startstr 分离部分的开始标记
  * @param $endstart 分离部分的结束标记
  * @return boolean  操作成功返回true
  */
 
  function getcontent ( $sourcestr, $startstr, $endstart )
  {
   $s = preg_quote( decode( $startstr ) );
   $e = preg_quote( decode( $endstart ) );
   $s = str_replace( " ", "[[:space:]]", $s );
   $e = str_replace( " ", "[[:space:]]", $e );
   $s = str_replace( " ", "[[:cntrl:]]", $s );
   $e = str_replace( " ", "[[:cntrl:]]", $e );

   preg_match_all( "@" . $s . "(.*?)". $e ."@is", $sourceStr, $tpl );

   $content = $tpl[1];
   $content = implode( "", $content );
   return $content;
  }
 
  function cut ( $sourceStr, $startStr, $endStr )
  {
                                                return  cut( $sourceStr ,decode( $startStr )  ,decode( $endStr) );
                                    }
 
  /**
  * 得到只含有连接和内容的列表数组
  * @param $sList  页面列表源数据
  * @return array  列表段内容
  */
 
  function getSourceList ( $sList )
  {
   preg_match_all( "//i", $sList, $list );

   $list = $list[0];
//foreach($list as $l) echo $l;
                                                     if(!$list || !is_array($list)){
                                                                   return $this->getSourceListExtend($sList);
                                                     }else{
                                   return $this->getList ( $list );
                                                     }
                                                    
  }
 
                                   function getSourceListExtend($sList)
                                   {
                                                  $content=explode("",$sList);
                                                  for($i=0;$i                                                  {
                                                           $lists=explode("                                                           $list[]=$lists[1];
                                                  }

                                                           return $this->GetListExtend( $list );
                                   }
 
  /**
  * 得到列表内容
  * @param $list  列表段内容
  * @return array  含有标题和连接的数组
  */
 
  function getList ( $list )
  {
   for ( $i = 0; $i    {               
    //title
    preg_match_all( "/>(.*?)/i", $list[$i], $templ );

    //content
    preg_match_all( "/href=("|'|)(.*?)("|'|)/i", $list[$i], $tempc );
   
    //获取的数据正确
    if( !empty( $templ[1][0] ) && !empty( $tempc[2][0] ) )
    {
     if( 0 == strpos( $tempc[2][0], "/" ) )
     {
      preg_match( "@http://(.*?)/@i", $this->URL, $url );
      $tempc[2][0] = substr( $url[0], 0, strlen( $url[0] ) - 1 ) . $tempc[2][0];
     }
    
     $listContent[$i][0] = $templ[1][0];
      $listContent[$i][1] = $tempc[2][0];
    }
                                                     }
                                                     if(!$listContent || !is_array($listContent)){
                                                               return $this->GetListExtend ( $list );
                                                     }else{
             return $listContent;
                                                     }
  }

  function GetListExtend ( $list )
  {                 
                                                     $list=str_replace(""","",$list);
                                                     $list=str_replace("'","",$list);
                                                     $list=str_replace("=","",$list);
   for ( $i = 0; $i    {
    //content
    $temp_link=$this->cut($list[$i],"href"," ");
                                                                       echo $temp_link."
";
    //title
                                                                       if(eregi(">",$list[$i])){
                                                                              $temp_title=substr(strrchr($list[$i], ">"), 1 );
                                                                              $temp_title=preg_replace( "@@is","",$temp_title);
                                                                              $temp_title=str_replace( ">","",$temp_title);
                                                                              $temp_title=str_replace( "                                                                              if(!$temp_title)   $temp_title=$list[$i] ;
                                                                               $temp_title=preg_replace( "@@is","",$temp_title);
                                                                               $temp_title=str_replace( ">","",$temp_title);
                                                                               $temp_title=str_replace( "                                                                                echo $temp_title."
";
                                                                       }else{
                                                                             $temp_title=$list[$i];      
                                                                             $temp_title=preg_replace( "@@is","",$temp_title);
                                                                              $temp_title=str_replace( ">","",$temp_title);
                                                                              $temp_title=str_replace( "                                                                              echo $temp_title."
";
                                                                       }
    //获取的数据正确
    if( !empty( $temp_link ) && !empty( $temp_title) )
    {
     if( 0 == strpos( $tempc[2][0], "/" ) )
     {
      preg_match( "@http://(.*?)/@i", $this->URL, $url );
      $temp_link = substr( $url[0], 0, strlen( $url[0] ) - 1 ) . $temp_link;
     }
    
     $listContent[$i][0] = trim($temp_title);
     $listContent[$i][1] = $temp_link;
    }
                                                     }
   return $listContent;

                                   }


  /**
  * 得到正文中的图片路径信息
  * @param $content 正文信息
  * @return array  信息中图片路径的数组
  */
 
  function getImageList ( $content )
  {
   preg_match_all( "/src=("|')(.*?)("|')/i", $content, $temp );
  
   $imageList = $temp[2];
   return array_unique($imageList);
  }
 
 
  /**
  * 下载图片时将页面中的路径替换成新的路径
  * @param $content  需要替换路径的页面内容
  * @return String   替换后的页面内容
  */
 
  function replaceImageParh ( $content )
  {
   for ( $i = 0; $i ImageList ); $i++ )
   {
                                                                      if($this->FileName[$i]){
                  $content = str_replace( $this->ImageList[$i], $this->imageURL.$this->FileName[$i], $content );
                                                                      }else{    
                                                                                    //$s=" /src=(\"|')".preg_quote($this->ImageList[$i])."(\"|')/i";
                  $content = str_replace($this->ImageList[$i], $GLOBALS[SET][webpath]."images/nopic.gif", $content );
                                                                      }
   }
  
   return $content;
  }
 
 
  /**
  * 下载图片时读取图片文件后存储在相应路径
  * @param $imageURL 需要读取的图片文件
  * @return boolean  操作成功返回true
  */
 
  function saveImage ( $imageURL )
  {
  
   for ( $i = 0; $i    {
    $fName = $this->saveFile( $imageURL[$i] );
    if( !empty( $fName ) )
    {               
     $filename[$i] = $fName;
    }
   }
  
   return $filename;
  }
 
 
  function saveFile( $fileName )
  {
  
   $s_filename = basename( $fileName );
   $ext_name = strtolower( strrchr( $s_filename, "." ) );
  
   if( ( ".jpg" && ".gif" && ".swf" ) != strtolower( $ext_name ) )
   {
    return "";
   }
  
  
   if( 0 == strpos( $fileName, "/" ) )
   {
    preg_match( "@http://(.*?)/@i", $this->URL, $url );
    $url = $url[0];
   }
  
   if( 0 == strpos( $fileName, "." ) )
   {
    $url = substr( $this->URL, 0, strrpos( $fileName, "/" ) );
   }
  
   $contents = @file_get_contents( $url . $fileName );

JenMusic
JenMusic

JenMusic是一款强调授权训练和高保真输出的 AI 音乐生成平台。

下载

   $s_filename = time(). rand( 1000, 9999 ) . $ext_name;
  
   //file_put_contents( $this->saveImagePath.$s_filename, $contents );
  
   $handle = @fopen ( $this->saveImagePath.$s_filename, "w" );
   @fwrite( $handle, $contents );
   @fclose($handle);
   if(filesize($this->saveImagePath.$s_filename)>3072){
             return $s_filename;
                                                     }else{
                                                               @unlink($this->saveImagePath.$s_filename);
             return "";
                                                    }
  
  }
 
  /**
  * 不下载图片则格式化其路径为绝对路径
                                   * 不能格式化变态路径 Eg: ./../  or /./../ 一类的  不过不影响结果
  * @param $imageURL 需要读取的图片文件
  * @return $filename  返回格式化的图片路径
  */
                                   function  ToPath($imageURL)
                                   {
                                                     $PathArray=parse_url($this->URL);
                                                     $webpath=$PathArray[scheme]."://".$PathArray[host] ;
                                                     $filepath=$PathArray[path] ;
                                           for ( $i = 0; $i    {
                                                                if( substr( $imageURL[$i] ,0,1 )== '/' ){
                                                                             $filename[$i] =$webpath.$imageURL[$i];
                                                                }elseif( substr( $imageURL[$i] ,0,2 )== './' ){
                                                                             $filename[$i] =$webpath.$filepath.substr( $imageURL[$i] ,1, strlen( $imageURL[$i]) );
                                                                }elseif( substr( $imageURL[$i] ,0,3 )== '../' ){
                                                                             $index=strrchr($filepath,"/");
                                                                             $filename[$i] =$webpath.substr($filepath,0,$index).substr($imageURL[$i] ,2, strlen( $imageURL[$i]));
                                                                }elseif(substr( $imageURL[$i] ,0,4)== 'http'){
                                                                             $filename[$i] =$imageURL[$i] ;
                                                                }else{
                                                               
                                                                }
   }
  
   return $filename;                    

                                   }

  /**
  * 不下载图片时将页面中的路径替换成新的路径
  * @param $content  需要替换路径的页面内容
  * @return String   替换后的页面内容
  */
                                  function ImgPathReplace( $content )
                                  {
   for ( $i = 0; $i ImageList ); $i++ )
   {
    $content = str_replace( $this->ImageList[$i], $this->FileName[$i], $content );
   }
  
   return $content;               
                                  }   
 
  function setURL ( $u )
  {
   $this->URL = $u;
   return true;
  }
 
  function setStartFlag ( $s )
  {
   $this->startFlag = $s;
   return true;
  }
 
  function setEndFlag ( $e )
  {
   $this->endFlag = $e;
   return true;
  }
 
  function setSaveImagePath ( $p )
  {
   $this->saveImagePath = $p;
   return true;
  }
 
  function setImageURL ( $i )
  {
   $this->imageURL = $i;
   return true;
  }
 
 
 }

?>

 



相关文章

PHP速学视频免费教程(入门到精通)
PHP速学视频免费教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
Go-Zero API开发使用教程
Go-Zero API开发使用教程

本专题聚焦Go-Zero的API开发,拆解.api描述文件的语法结构、路由与分组定义、参数绑定与校验规则,说明统一响应体、JWT鉴权、跨域与接口文档生成的落地方式,并给出网关聚合与接口测试的实践建议。

2026.11.12

140

15

Go-Zero安装与goctl环境配置教程
Go-Zero安装与goctl环境配置教程

本专题围绕Go-Zero的安装与工具链配置展开,讲清Go版本要求、goctl安装方式、代理镜像、protoc依赖与环境变量设置,说明多版本共存、升级回退与CI流水线安装的做法,并整理安装阶段的常见报错与验证步骤。

2026.11.12

100

15

Go-Zero零基础入门教程
Go-Zero零基础入门教程

本专题整理Go-Zero的入门知识,从框架定位、环境准备到跑通第一个API服务,讲清目录分层、配置加载、日志与中间件的基本用法,并对比Gin、Kratos的差异,帮助Go开发者建立整体认知,判断它是否适合当前项目。

2026.11.12

80

15

Postman身份认证怎么配置
Postman身份认证怎么配置

本专题从实际API接口调试场景出发,讲解Postman身份认证的完整配置流程,包括常见认证协议介绍、授权参数设置、访问令牌填写、OAuth 2.0授权及认证失败排查,帮助用户正确完成接口授权与访问验证。

2026.11.06

160

12

Postman请求头Headers怎么设置
Postman请求头Headers怎么设置

围绕Postman HTTP请求头配置展开,讲解Headers选项的具体使用方式,包括Key和Value参数填写、请求头信息添加、常见Header字段设置及请求头修改方法,帮助用户解决接口调试过程中遇到的请求头配置问题。

2026.11.06

140

13

Postman接口请求发送教程
Postman接口请求发送教程

详细介绍Postman接口请求的创建与发送方法,涵盖GET和POST请求、URL配置、请求参数、Headers请求头、Body请求体设置以及响应结果查看,帮助用户掌握Postman接口请求的完整操作流程。

2026.11.06

120

10

DBeaver怎么导入数据
DBeaver怎么导入数据

DBeaver数据导入专题,整理Excel、CSV、SQL、TXT等常见文件的数据导入方法,涵盖目标数据库选择、数据表创建、字段映射、数据类型设置及导入参数配置等操作,帮助用户将本地文件中的数据写入数据库,并了解批量导入、重复数据处理和导入失败等常见问题的解决方法。

2026.11.05

120

12

DBeaver安全防护教程大全
DBeaver安全防护教程大全

DBeaver安全防护专题,围绕数据库连接安全、账号密码保护和访问权限管理展开,整理SSL/TLS加密连接、SSH隧道、数据库凭据保存及证书验证等配置方法,同时介绍敏感信息保护、远程连接风险和高权限账号使用等注意事项,帮助用户提高数据库日常管理的安全性。

2026.11.05

100

12

DBeaver连接创建与配置教程
DBeaver连接创建与配置教程

本专题聚焦DBeaver数据库连接配置与管理,覆盖从新建连接、选择数据库类型到填写连接参数和验证连接的完整流程。针对MySQL、PostgreSQL、SQL Server等常见数据库,整理相应的连接设置方法,并提供驱动缺失、认证失败和连接异常等问题的排查方向。

2026.11.04

120

16

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程