PHP采集的实现,直接写成个类

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS PostgreSQL,集群系列 2核4GB
简介:

   

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/**
coder:qiuyunbest@163.com
*/
class  GetProduct{
   public  $url ;
                                                     
   function  __construct( $url = '' ){
                                                     
     $this ->url =  $url ;
                                                     
   }
   function  curl_file_get_contents(){
     $ch  = curl_init();
     curl_setopt( $ch , CURLOPT_URL,  $this ->url);
     curl_setopt( $ch , CURLOPT_TIMEOUT, 60);
     curl_setopt( $ch , CURLOPT_USERAGENT, _USERAGENT_);
     curl_setopt( $ch , CURLOPT_REFERER,_REFERER_);
     curl_setopt( $ch , CURLOPT_RETURNTRANSFER, 1);
     $r  = curl_exec( $ch );
     curl_close( $ch );
     return  $r ;
                                                       
   }
                                                     
   function  GetAll( $preg , $a = '' , $html = '' ){
                                                     
     if ( empty ( $html )){
                                                       
       $html  $this ->curl_file_get_contents();
                                                       
     }
                                                     
     preg_match_all( "^" . $preg . "^isU" , $this ->curl_file_get_contents(), $title );
                                                       
     if ( $a ===0 || ! empty ( $a )){
                                                       
       return  $title [ $a ];
                                                       
     } else {
                                                       
       return  $title ;
                                                       
     }
                                                     
   }
                                                     
                                                     
   function  GetOne( $preg , $a = '' , $html = '' ){
                                                     
     if ( empty ( $html )){
                                                       
       $html  $this ->curl_file_get_contents();
                                                       
     }
                                                     
     preg_match( "^" . $preg . "^isU" , $html , $title );
                                                       
     if ( $a ===0 || ! empty ( $a )){
                                                       
       return  $title [ $a ];
                                                       
     } else {
                                                       
       return  $title ;
                                                       
     }
                                                     
   }
                                                     
   function  str_r( $str ){
                                                     
     $str  str_replace ( "$" , "" , $str );
                                                       
     $str  str_replace ( "<br />" , "<br/>" , $str );
                                                       
     $str  strip_tags ( $str , "<br/>" );
                                                       
     $str  str_replace ( "\"" , "\\\"" , $str );
                                                       
     $str  str_replace ( "&euro;" , "" , $str );
                                                       
     $str  str_replace ( "amp;" , "" , $str );
                                                       
     $str  str_replace ( "\r\n" , "" , $str );
                                                       
     return  $str ;
                                                     
   }
                                                    
                                                     
                                                     
   function  Load( $id , $allnum ){
                                                     
     $load = round ( $id / $allnum *100,2). "%" ;
                                                       
     $loading  "正在采集请稍后......." . $id . "/" . $allnum . ".......<div style=\"width:600px;border:1px solid #ccc\"><div style=\"width:" . $load . ";text-align:center;height:14px;font-size:14px;background-color:#999\">" . $load . "</div></div>" ;
                                                       
     return  $loading ;
                                                     
   }
                                                     
   function  getimg( $purl , $filename ){
                                                     
         if ( $purl == "" )
         {
             return  false;
         }
         $ext = strrchr ( $purl , "." );
         if ( $ext != ".gif"  $ext != ".bmp"  $ext != ".jpg"  $ext != ".png"  $ext != ".GIF"  $ext != ".BMP"  $ext != ".JPG" )
         {
             return  false;
         }
         //$img=file_get_contents($purl) or die("í¼Æ¬′ò2»¿a");
                                                           
         if ( file_exists ( $filename ))
             return  true;
         /*
         $ctx = stream_context_create(array(
                 'http' => array(
                 'timeout' => 160
                 )
                 )
             );        
         $img=file_get_contents($purl,0,$ctx);
         */
                                                           
         try {
         $ch  = curl_init();
             $timeout  = 11160;
             curl_setopt ( $ch , CURLOPT_URL,  $purl );
             curl_setopt ( $ch , CURLOPT_RETURNTRANSFER, 1);
             curl_setopt ( $ch , CURLOPT_CONNECTTIMEOUT,  $timeout );
             $img  = curl_exec( $ch );
             curl_close( $ch );
         }
         catch (Exception  $e )
         { return  false;}
                                                               
         if ( $img == "" )
             return  false;
                                                               
         $fp =@ fopen ( $filename , "a" );
         fwrite( $fp , $img );
         fclose( $fp );
         return  true;
     }
                                                       
     function  Jump( $id , $num ){
                                                       
     if ( $id < $num ){
                                                       
       $id ++;
                                                         
       $jump  "<script>location.href='?id=" . $id . "'</script>" ;
                                                         
       return  $jump ;
                                                       
     } else {
                                                       
       return  "采集完成!" ;
                                                       
     }
     }
                                                     
}


数据库类:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
class  Mysql{
   private  $host ;
                                          
   private  $user ;
                                          
   private  $pwd ;
                                          
   function  __construct( $host , $user , $pwd , $db , $un = "utf8" ){
                                          
     $this ->host =  $host ;
                                            
     $this ->user =  $user ;
                                            
     $this ->pwd =  $pwd ;
                                            
     $this ->db =  $db ;
                                            
     $this ->un =  $un ;
                                            
     $this ->conn();
                                          
   }
                                          
   function  conn(){
                                          
     $conn  = mysql_connect( $this ->host, $this ->user, $this ->pwd)  or  die (( "ᬽó꧰ü" ).mysql_error());
                                            
     mysql_select_db( $this ->db, $conn or  die ( "êy¾Y¿a2»′æÔú" .mysql_error());
                                            
     mysql_query( "set character set $this->un" );
                                            
     mysql_query( "set names $this->un" );
                                          
   }
                                          
   function  select( $table , $where = "" , $num =false){
                                          
     $query  = mysql_query( "select * from `$table` $where" );
                                            
     while ( $row  = mysql_fetch_array( $query )){
                                            
       $arr [] =  $row ;
                                            
     }
                                            
     if ( $num ){
                                            
       return  $arr [0];
                                            
     } else {
                                            
       return  $arr ;
                                            
     }
                                          
   }
                                          
   function  num_rows( $table , $where = "" ){
                                          
     $query  = mysql_query( "select * from `$table` $where" );
                                            
     $num  = mysql_num_rows( $query );
                                            
     return  $num ;
                                          
   }
                                          
   function  insert( $table , $name = "" , $value ){
                                          
     mysql_query( "INSERT INTO `$table` ($name) VALUES ($value)" );
                                          
   }
                                          
   function  update( $table , $fn , $where = "" ){
                                          
     mysql_query( "UPDATE $table SET $fn $where" );
                                          
   }
                                          
   function  close(){
                                                
         return  mysql_close();
                                            
     }
                                          
                                          
}









本文转自 3147972 51CTO博客,原文链接:http://blog.51cto.com/a3147972/1222037,如需转载请自行联系原作者
目录
相关文章
|
4月前
|
Java 程序员 PHP
PHP对象和类
PHP对象和类
45 0
|
3天前
|
PHP
PHP中的面向对象编程:理解类与对象
本文将深入探讨PHP中面向对象编程的核心概念——类与对象。通过实例讲解,帮助读者更好地理解如何在PHP中运用OOP编写更高效、可维护的代码。
20 9
|
16天前
|
PHP
PHP全自动采集在线高清壁纸网站源码
PHP全自动采集在线高清壁纸网站源码,PHP全自动采集在线高清壁纸网站源码,一款开源壁纸源码,无需安装。集合360壁纸,百度壁纸,必应壁纸,简单方便。每天自动采集,自动更新,非常不错,php源码 网站源码 免费源码 自动采集。
32 3
|
28天前
|
PHP 开发者
PHP中的面向对象编程:掌握类与对象的精髓
探索PHP的面向对象编程世界,本文将带你了解如何通过创建和操作类来实例化对象。我们将深入讲解类的声明、构造函数的使用以及继承和多态性的概念。准备好,让我们一起在代码的海洋中航行,揭开PHP对象编程的神秘面纱!
|
2月前
|
PHP
如何在PHP中创建一个自定义的观察者类?
【7月更文挑战第2天】如何在PHP中创建一个自定义的观察者类?
18 0
|
2月前
|
PHP
PHP中,你可以使用class关键字来定义一个类
【7月更文挑战第2天】PHP中,你可以使用class关键字来定义一个类
19 0
|
4月前
|
监控 PHP Python
1688快速获取整店铺列表 采集接口php Python
在电子商务的浪潮中,1688平台作为中国领先的批发交易平台,为广大商家提供了一个展示和销售商品的广阔舞台;然而,要在众多店铺中脱颖而出,快速获取商品列表并进行有效营销是关键。
|
4月前
|
存储 监控 安全
PHP医院安全(不良)事件报告系统源码 vue2+element支持11大类不良事件上报、审核处理、分析改进
医院安全(不良)事件管理系统采用无责的、自愿的填报不良事件方式,有效地减轻医护人员的思想压力,实现以事件为主要对象,可以自动、及时、实际地反应医院的安全、不良、近失事件的情况,更好地掌握不良事件的发生趋势,为及时采取适当的管理措施和流程、制度改进提供了良好的量化依据。系统通过汇集不同类型事件的报告,从中分析出医院内部潜在的问题和风险,将发生的事故降到最低,从而保证病人安全和医护人员安全。
46 0
|
4月前
|
自然语言处理 小程序 API
基于Python采集136个PHP源码
基于Python采集136个PHP源码
32 1
|
4月前
|
缓存 前端开发 搜索推荐
分享110个采集小偷PHP源码,总有一款适合你
分享110个采集小偷PHP源码,总有一款适合你
221 1